Zend Framework 2和Doctrine 2 - 多个数据库的配置

时间:2014-06-04 16:05:22

标签: php zend-framework doctrine-orm zend-framework2

我将configuration.md文件中的代码粘贴到

module.config.php

'doctrine' => array(
        'connection' => array(
            'orm_crawler' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'root',
                    'password' => 'root',
                    'dbname'   => 'crawler',
                    'driverOptions' => array(
                        1002 => 'SET NAMES utf8'
                    ),
                )
            )
        ),

        'configuration' => array(
            'orm_crawler' => array(
                'metadata_cache'    => 'array',
                'query_cache'       => 'array',
                'result_cache'      => 'array',
                'driver'            => 'orm_crawler',
                'generate_proxies'  => true,
                'proxy_dir'         => 'data/DoctrineORMModule/Proxy',
                'proxy_namespace'   => 'DoctrineORMModule\Proxy',
                'filters'           => array()
            )
        ),

        'driver' => array(
            'Crawler_Driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(
                    __DIR__ . '/../src/Crawler/Entity'
                )
            ),
            'orm_crawler' => array(
                'class'   => 'Doctrine\ORM\Mapping\Driver\DriverChain',
                'drivers' => array(
                    'Crawler\Entity' =>  'Crawler_Driver'
                )
            ),
        ),

        'entitymanager' => array(            
            'orm_crawler' => array(
                'connection'    => 'orm_crawler',
                'configuration' => 'orm_crawler'
            )
        ),

        'eventmanager' => array(
            'orm_crawler' => array()
        ),

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

        'entity_resolver' => array(
            'orm_crawler' => array()
        ),

    ),

```

Module.php

public function getServiceConfig()
{
    return array(
        'factories' => array(
'doctrine.authenticationadapter.orm_crawler'  => new \DoctrineModule\Service\Authentication\AdapterFactory('orm_crawler'),
                'doctrine.authenticationstorage.orm_crawler'  => new \DoctrineModule\Service\Authentication\StorageFactory('orm_crawler'),
                'doctrine.authenticationservice.orm_crawler'  => new \DoctrineModule\Service\Authentication\AuthenticationServiceFactory('orm_crawler'),

                'doctrine.connection.orm_crawler'             => new \DoctrineORMModule\Service\DBALConnectionFactory('orm_crawler'),
                'doctrine.configuration.orm_crawler'          => new \DoctrineORMModule\Service\ConfigurationFactory('orm_crawler'),
                'doctrine.entitymanager.orm_crawler'          => new \DoctrineORMModule\Service\EntityManagerFactory('orm_crawler'),

                'doctrine.driver.orm_crawler'                 => new \DoctrineModule\Service\DriverFactory('orm_crawler'),
                'doctrine.eventmanager.orm_crawler'           => new \DoctrineModule\Service\EventManagerFactory('orm_crawler'),
                'doctrine.entity_resolver.orm_crawler'        => new \DoctrineORMModule\Service\EntityResolverFactory('orm_crawler'),
                'doctrine.sql_logger_collector.orm_crawler'   => new \DoctrineORMModule\Service\SQLLoggerCollectorFactory('orm_crawler'),
                'doctrine.mapping_collector.orm_crawler'      => function (\Zend\ServiceManager\ServiceLocatorInterface $sl) {
                    $em = $sl->get('doctrine.entitymanager.orm_crawler');

                    return new \DoctrineORMModule\Collector\MappingCollector($em->getMetadataFactory(), 'orm_crawler_mappings');
                },
                'DoctrineORMModule\Form\Annotation\AnnotationBuilder' => function(\Zend\ServiceManager\ServiceLocatorInterface $sl) {
                    return new \DoctrineORMModule\Form\Annotation\AnnotationBuilder($sl->get('doctrine.entitymanager.orm_crawler'));
                },
        ),
    );
}

我收到以下错误:

C:\xampp\vhosts\zf2-trade\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:529

消息:

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getServiceManager

我做错了什么?请帮忙。

关心马修

3 个答案:

答案 0 :(得分:10)

Mac,欢迎来到stackoverflow!您不需要分别为每个连接定义自定义工厂。 DoctrineORMModule已经为我们处理了这项工作。

当您需要实体管理器时,通过在别名中使用它们的名称从服务定位器实例获取它,如下所示:

$this->getServiceLocator()->get('doctrine.entitymanager.orm_default');

$this->getServiceLocator()->get('doctrine.entitymanager.orm_alternative');

我正在分享我当前使用PostgreSQL和MySQL连接的当前应用程序的数据库配置之一。

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            // Default DB connection
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
                'params' => array(
                    'host' => '1.2.3.4',
                    'user' => 'pdbuser',
                    'port' => '5432',
                    'password' => '****',
                    'dbname' => 'mydb',
                    'driver' => 'pdo_pgsql',
                ),
            ),

            // Alternative DB connection
            'orm_alternative' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host' => '4.5.6.7',
                    'user' => 'dbuser',
                    'port' => '3306',
                    'password' => '****',
                    'dbname' => 'mydb',
                    'driver' => 'pdo_mysql',
                ),
            ),
        ),

        // Entity Manager instantiation settings
        'entitymanager' => array(
            'orm_default' => array(
                'connection'    => 'orm_default',
                'configuration' => 'orm_default',
            ),
            'orm_alternative' => array(
                'connection'    => 'orm_alternative',
                'configuration' => 'orm_alternative',
            ),
        ),

        // Use array cache locally, also auto generate proxies on development environment.
        'configuration' => array(
            'orm_default' => array(
                'metadata_cache' => 'array',
                'query_cache' => 'array',
                'result_cache' => 'array',
                'hydration_cache' => 'array',
                'generate_proxies' => true,
            ),
            'orm_alternative' => array(
                'metadata_cache' => 'array',
                'query_cache' => 'array',
                'result_cache' => 'array',
                'hydration_cache' => 'array',
                'generate_proxies' => true,
            ),
        ),
    ),
);

您可以轻松地将此配置与您的配置合并。

希望它有所帮助。

答案 1 :(得分:0)

@foozy:这正是我所期待的。随着

./vendor/bin/doctrine-module orm:schema-tool:update --force

您现在可以更新或创建数据库架构。

我的问题:如何定义应在哪个db中创建哪个实体? 关心安德烈

答案 2 :(得分:0)

@edigu答案可以很好地工作,但在某些情况下,它可以 “以下错误:Zend \ ServiceManager \ ServiceManager :: get无法获取或创建doctrine.connection.orm_crawle的实例”

为解决此问题,我们可能会更改“实体管理器”设置

'entitymanager' => array(
        'orm_default' => array(
            'connection'    => 'orm_default',
            'configuration' => 'orm_default',
        ),
        'orm_alternative' => array(
            'connection'    => 'orm_alternative',
            'configuration' => 'orm_default',  //<--use parent configurations 
        ),
    ),

进行推荐检查here