本文介绍了使用Zend Framework 2和DoctrineORMModule连接到多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用Zend Framework 2和DoctrineORMModule。我需要访问不同的数据库连接并映射两个不同的模式集。

Hello i'm using Zend Framework 2 and DoctrineORMModule. I need to access to different data bases connections and map two different set of schemas.

'doctrine' => array(
    'connection' => array(
        'orm_default' => array(
            'driverClass' => 'Doctrine\DBAL\Driver\PDODblib\Driver',
            'params' => array(
                'host'     => 'HOST',
                'port'     => '1433',
                'user'     => 'USER',
                'password' => 'PASS',
                'dbname'   => 'DBNAME',
            )
        )
    )
),
/////////////

'doctrine' => array(
        'connection' => array(
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => '127.0.0.1',
                    'port'     => '3306',
                    'user'     => 'root',
                    'password' => 'root',
                    'dbname'   => 'test',
                )
            )
        ),
    ),



我在文档中找到了:

但它不是很描述。

I found this in the documentation:https://github.com/doctrine/DoctrineORMModule/blob/master/docs/configuration.md#how-to-use-two-connectionsBut it is not very descriptive.

任何人都可以帮助我?

推荐答案

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\DBAL\Connection;

/**
 * @author  Rafał Książek
 */
class DbFactory
{

    /**
     * @var array
     */
    protected $config;

    /**
     * @return array
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->config = $config;
    }

    /**
     * Create connection to database
     *
     * @param string $dbName
     * @return \Doctrine\DBAL\Connection
     * @throws \InvalidArgumentException
     * @throws \Exception
     */
    public function getConnectionToDatabase($dbName)
    {
        $config = $this->getConfig();

        if (empty($config['doctrine']['connection']['orm_default']['params'])) {
            throw new \InvalidArgumentException('There is insufficient data in the configuration file!');
        }

        $params           = $config['doctrine']['connection']['orm_default']['params'];
        $params['dbname'] = $dbName;
        $params['driver'] = 'pdo_mysql';

        if (!($dbConnection = DriverManager::getConnection($params)))
            throw new \Exception('There was a problem with establish connection to client db');

        return $dbConnection;
    }

    /**
     *
     * @param \Doctrine\DBAL\Connection $dbConnection
     * @param \Doctrine\ORM\Configuration $config
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager(Connection $dbConnection, Configuration $config)
    {
        return EntityManager::create($dbConnection, $config);
    }

}

如何使用:

$applicationConfig = $sm->get('config');
$em = $sm->get('Doctrine\ORM\EntityManager');
$emDefaultConfig = $em->getConnfiguration();

$dbFactory = new DbFactory($applicationConfig);
$anotherConnection = $dbFactory->getConnectionToDatabase('another_db');
$anotherEntityManager = $dbFactory->getEntityManager($anotherConnection, $emDefaultConfig);

$usersOnAnotherDb = $anotherEntityManager->getRepository('Application\Entity\User')->findAll();

这篇关于使用Zend Framework 2和DoctrineORMModule连接到多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:59