与Doctrine 2和ZF2的辅助数据库连接

时间:2014-12-27 23:26:37

标签: php doctrine-orm zend-framework2

我正在尝试为我的学说配置添加额外的连接。我的orm_default连接工作得非常好,现在我正在尝试添加一个带有自己的Doctrine配置的新模块(主要是学习目的,但它相当烦人,我无法让它工作)。

该模块名为Frontpage,除了驻留在local.php中的用户名/密码详细信息外,所有相关代码都在此页面中...

我的错误是

Zend\ServiceManager\Exception\ServiceNotCreatedException
An exception was raised while creating "doctrine.entitymanager.orm_hosts"; no instance returned

此外,我认为相关的堆栈跟踪(最后一个例外)也是如此,但不知道如何解决...

Zend\Stdlib\Exception\BadMethodCallException
The option "hydration_cache" does not have a matching setHydrationCache setter method which must be defined

这是我的模块配置(相关部分)文件:

'doctrine' => [
    'connection' => [
        'orm_hosts' => [
            'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'params' => [
                'host' => '127.0.0.1',
                'port' => '3306',
                'dbname' => 'hosts',
            ],
        ],
    ],
    'entitymanager' => array(
        'orm_hosts' => array(
            'connection'    => 'orm_hosts',
            'configuration' => 'orm_hosts'
        )
    ),
    'configuration' => array(
        'orm_hosts' => array(
            'driver'            => 'orm_hosts',
            'generate_proxies'  => true,
            'proxy_dir'         => 'data/DoctrineORMModule/Proxy',
            'proxy_namespace'   => 'DoctrineORMModule\Proxy',
            'filters'           => array(),
            'metadata_cache'    => 'array',
            'query_cache'       => 'array',
            'result_cache'      => 'array',
            //'hydration_cache'   => 'array',
        )
    ),

    'driver' => array(
        'orm_hosts' => array(
            'class'   => 'Doctrine\ORM\Mapping\Driver\DriverChain',
            'drivers' => array(
                'Common\Entity' =>  'Hosts_Driver'
            )
        ),
        'Hosts_Driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(
                __DIR__ . '/../src/Common/Entity'
            )
        ),
    ),
    'eventmanager' => array(
        'orm_hosts' => array()
    ),

    'sql_logger_collector' => array(
        'orm_hosts' => array(),
    ),

    'entity_resolver' => array(
        'orm_hosts' => array()
    ),
],

我的Module.php的getServiceConfig():

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'doctrine.connection.orm_hosts'           => new Service\DBALConnectionFactory('orm_hosts'),
            'doctrine.configuration.orm_hosts'        => new Service\ConfigurationFactory('orm_hosts'),
            'doctrine.entitymanager.orm_hosts'        => new Service\EntityManagerFactory('orm_hosts'),

            'doctrine.entity_resolver.orm_hosts'      => new Service\EntityResolverFactory('orm_hosts'),
            'doctrine.sql_logger_collector.orm_hosts' => new Service\SQLLoggerCollectorFactory('orm_hosts'),
            'doctrine.driver.orm_hosts'               => new \DoctrineModule\Service\DriverFactory('orm_hosts'),
            'doctrine.eventmanager.orm_hosts'         => new \DoctrineModule\Service\EventManagerFactory('orm_hosts'),

            'DoctrineORMModule\Form\Annotation\AnnotationBuilder\orm_hosts' => function(\Zend\ServiceManager\ServiceLocatorInterface $sl) {
                return new \DoctrineORMModule\Form\Annotation\AnnotationBuilder($sl->get('doctrine.entitymanager.orm_hosts'));
            },
        ),
    );
}

这是我在IndexController中的getEntityManager()失败

/**
 * @return array|EntityManager|object
 */
public function getEntityManager() {
    if (NULL === $this->em) {
        /** @var \Doctrine\ORM\EntityManager $em */
        $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_hosts');
        //$em = $this->getServiceLocator()->get('doctrine')->getManager("orm_hosts");
        $this->em = $em;
    }
    return $this->em;
}

任何帮助都会得到很好的赞赏:)

祝你好运 理查德

1 个答案:

答案 0 :(得分:0)

好的,所以我仍然不知道上面的代码有什么问题,但如果我删除

'Hydration_cache' => 'array'

在我的模块配置的学说配置中,它确实有效!不过,如果有人想要解释会发生什么,我会很感激地知道:)

相关问题