Zend Framework 2 and Doctrine 2 - Configuration for multiple databases

后端 未结 3 1529
独厮守ぢ
独厮守ぢ 2021-01-03 07:24

I pasted the code from the configuration.md file to

module.config.php

\'doctrine\' => array(
        \'connection\' => array(
            \'orm         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-03 07:34

    Mac, welcome to stackoverflow! You don't need to define custom factories for each connection respectively. DoctrineORMModule already handles this job for us.

    When you need the entity managers, get it from service locator instance by using their names in the alias like this:

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

    or

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

    I'm sharing one of my current application's database configuration which currently uses both PostgreSQL and MySQL connections.

     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,
                ),
            ),
        ),
    );
    

    You can easily merge this configuration with yours.

    Hope it helps.

提交回复
热议问题