Zend Framework 2 and Doctrine 2 - Configuration for multiple databases

后端 未结 3 1505
独厮守ぢ
独厮守ぢ 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:32

    @edigu answer works perfectly fine but in some cases, it gives "Following error: Zend\ServiceManager\ServiceManager:: get was unable to fetch or create an instance for doctrine.connection.orm_crawle"

    So to resolve this we may change in the Entity Manager settings

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

    for refernece check here

    0 讨论(0)
  • 2021-01-03 07:33

    @foozy: this is exactly what I'm looking. With

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

    you can now update or create the database schema.

    My question: how to define which entity should be created in which db? Regards Andrea

    0 讨论(0)
  • 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.

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

    You can easily merge this configuration with yours.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题