Doctrine 2,从PDO创建EntityManager - 错误

时间:2014-01-23 23:34:49

标签: php pdo doctrine-orm entitymanager

我有以下情况:

class ModelManager extends EntityManager implements ModelManagerInterface
{

    public function __construct($conn, $options = array())
    {
        $isDev = empty($options['is_dev']) ? false : $options['is_dev'];

        // @see Doctrine\ORM\EntityManager#create()
        $config = ArrayUtils::arrayMergeRecursiveRight($options, array(
            'cache' => $isDev ? new Cache\ArrayCache : new Cache\ApcCache,
            'queryCache' => $isDev ? new Cache\ArrayCache : new Cache\ApcCache,
            'modelsProxiesPath' => getcwd() . '/cache/ModelProxies',
        ));

        // create Configuration object
        if (is_array($options)) {
            $config = $this->initConfig($options);
            if (!$config->getMetadataDriverImpl()) {
                throw ORMException::missingMappingDriverImpl();
            }
        }

        // test EventManager object
        $eventManager = empty($options['eventManager']) ? new EventManager() : $options['eventManager'];

        switch (true) {
            case ($conn instanceof \PDO):
                $conn = array(
                    'driver' => 'pdo_' . $conn->getAttribute(\PDO::ATTR_DRIVER_NAME),
                    'pdo' => $conn,
                );
                print_r($conn);
                $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
                break;

            case (is_string($conn)):
                $pdo = new \PDO($conn);
                if ('oci8' !== $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
                    $conn = array(
                        'driver' => 'pdo_' . $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME),
                        'pdo' => $pdo,
                    );
                } else {
                    $conn = $this->mapDsnToDoctrine($conn);
                }
                $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
                break;

            case (is_array($conn)):
                $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
                break;

            case ($conn instanceof Connection):
                if ($eventManager !== null && $conn->getEventManager() !== null) {
                    throw ORMException::mismatchedEventManager();
                }
                break;

            default:
                throw new Exception\InvalidArgument("Invalid argument: " . json_encode($conn));
        }

        // parent constructor
        // @see Doctrine\ORM\EntityManager#__construct()
        parent::__construct($conn, $config, $conn->getEventManager());
    }
// ...
}

像这样使用smth:

self::$em = new ModelManager(
    new \PDO(self::$config['db_adapter']['connection']['dsn']),
    empty(self::$config['db_adapter']['options']['doctrine']) ?
        array() : self::$config['db_adapter']['options']['doctrine']
);

$tool = new \Doctrine\ORM\Tools\SchemaTool(self::$em);
$classes = $this->db(self::$config['db_adapter']['options']['doctrine']['modelsPath']);
$tool->updateSchema($classes);

配置如下所示:

$config = array(
    'db_adapter' => array(
        'connection' => array(
            'dsn' => 'mysql://root:******@localhost/netistest' . time(),
        ),
        'options' => array(
            'doctrine' => array(
                'cache' => 'ArrayCache|ApcCache', // development|testing&production
                'queryCache' => 'ArrayCache|ApcCache', // development|testing&production
                'modelsPath' => __DIR__ . '/NetisTest/Model',
                'modelsProxiesPath' => __DIR__ . '/cache/doctrine/ModelProxies',
                'modelsProxiesNamespace' => 'App\Models\Proxies',
                'modelsProxiesAutogenerate' => 'App\Models\Proxies',
            ),
            'zend' => array(
                // at this moment we decided not to implement Zend\DbAdapter
            ),
        )
    ),
);

所有这些奇怪配置的问题在于,在实例化ModelManager类之后,当试图询问数据库以创建与我的模型关联的表时,它会抛出以下错误:

Doctrine\DBAL\DBALException: An exception occurred while executing 'SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'':

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:91
/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:699
/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:630
/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php:206
/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php:250
/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php:986
/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/SchemaTool.php:855
/home/dragosc/workspace/athem-repositories/Component_NetisDb/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/SchemaTool.php:832
/home/dragosc/workspace/athem-repositories/Component_NetisDb/tests/NetisTest/Db/Setup/Doctrine.php:51

您也可以在这里找到代码(不过是旧版本): https://github.com/athemcms/Component_NetisDb/blob/master/library/Netis/Db/ModelManager/ModelManager.php

任何人都可以帮助我理解为什么PDO不足以让EntityManager记住数据库吗?

提前多多感谢!

0 个答案:

没有答案