使用doctrine2在zf2中配置多个数据库

时间:2016-04-08 15:31:40

标签: doctrine-orm zend-framework2

如何使用doctrine 2在Zend Framework 2中配置(和使用)多个数据库?目前我在local.php中有这个:

return array(
'doctrine' => array(
    'connection' => array(
        // default connection name
        'orm_default' => array(
            'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'params' => array(
                'host'     => 'localhost',
                'port'     => '3306',
                'user'     => 'root',
                'password' => '',
                'dbname'   => 'data1',
                'charset' => 'utf8', 
                'driverOptions' => array(
                        1002=>'SET NAMES utf8'
                )
            )
        )
    )
),

);

但是我没有办法添加第二个。

1 个答案:

答案 0 :(得分:0)

也许这个例子可能会告诉你如何,这就是我在我的应用程序中配置两个数据库的方式,但我没有使用Doctrine。

DBs配置

return array(
    'doctrine' => array(
        'connection1' => array(
            // default connection name
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'root',
                    'password' => '',
                    'dbname'   => 'data1',
                    'charset' => 'utf8', 
                    'driverOptions' => array(1002 => 'SET NAMES utf8')
                )
            )
        ),

        'connection2' => array(
            // default connection name
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'root',
                    'password' => '',
                    'dbname'   => 'data2',
                    'charset' => 'utf8', 
                    'driverOptions' => array(1002 => 'SET NAMES utf8')
                )
            )
        )
    ),
);

然后,您需要创建一个连接到数据库connection1的DbAdapterFactory和另一个连接到数据库connection2的工厂:

PS:当然这些名字只是一些例子,你可以使用更好的名字。

class Adapter1Factory implements FactoryInterface
{
    /**
     * Create DbAdapter
     *
     * @param  ServiceLocatorInterface $serviceLocator
     * @return DbAdapter
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config    = $serviceLocator->get('config');
        $configDb1 = $config['connection1'];
        $adapter   = new \Zend\Db\Adapter\Adapter($configDb1);

        return $adapter;
    }
}

class Adapter2Factory implements FactoryInterface
{
    /**
     * Create DbAdapter
     *
     * @param  ServiceLocatorInterface $serviceLocator
     * @return DbAdapter
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config    = $serviceLocator->get('config');
        $configDb2 = $config['connection2'];
        $adapter   = new \Zend\Db\Adapter\Adapter($configDb2);

        return $adapter;
    }
}

现在只需使用特定的AdapterFactory即可连接到某个数据库。