Hi Hope you are well,

I want to use doctrine 2 for this project.

I want to connect multiple dynamic databases for my project, I have already 
connected multiple databases in my project, But I am struggling to connect 
it by dynamically, Please find my local.php file below, path of local.php 
file is

/var/www/xxxx/yyyy/config/autoload/local.php

local.php

return array(
'db' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:dbname=default_db_name;host=localhost',
    'username'       => 'default_user',
    'password'       => 'default_password',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
),
'doctrine' => array(
    'connection' => array(
        'orm_default' => array(
            'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
            'params' => array(
                'host' => 'localhost',
                'port' => '3306',
                'user' => 'default_user',
                'password' => 'default_password',
                'dbname' => 'default_db_name',
            ),
        ),
        'client_1' => array(
            'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
            'params' => array(
                'host' => 'localhost',
                'port' => '3306',
                'user' => 'user_1',
                'password' => 'password_1',
                'dbname' => 'client_1',
            ),
        ),
    ),
),
'doctrine' => array(
    'connection' => array(
        // Default DB connection
        'orm_default' => array(
            'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
            'params' => array(
                'host' => 'localhost',
                'port' => '3306',
                'user' => 'devuser',
                'password' => 'default_password',
                'dbname' => 'default_db_name',
                'driver' => 'pdo_mysql',
            ),
        ),

        'client_1' => array(
            'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
            'params' => array(
                'host' => 'localhost',
                'port' => '3306',
                'user' => 'user_1',
                'password' => 'password_1',
                'dbname' => 'client_1',
                'driver' => 'pdo_mysql',
            ),
        ),
    ),

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

    // 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,
        ),
        'client_1' => array(
            'metadata_cache' => 'array',
            'query_cache' => 'array',
            'result_cache' => 'array',
            'hydration_cache' => 'array',
            'generate_proxies' => true,
        ),
    ),
),
'zf-oauth2' => array(
    'storage' => 'ZF\\OAuth2\\Adapter\\PdoAdapter',
    'db' => array(
        'dsn_type' => 'PDO',
        'dsn' => 'mysql:host=localhost;dbname=default_db_name',
        'username' => 'default_user',
        'password' => 'default_password',
    ),
),
);

Now I am connecting client_1 database by following coding,

please find below file

File name: NotificationLogFactory.php Path of the file: 
/var/www/xxxx/yyyyy/module/Application/src/Application/Factory/NotificationLogFactory.php

<?php

namespace Application\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Service\NotificationLogService;

class NotificationLogFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
{        
    $em = $serviceLocator->get('doctrine.entitymanager.client_1');

    return new NotificationLogService($em);
}

}

My question is that how to connect dynamic multiple databases?

Example: client_1, client_2, client_3, client_4 ......

please let me know if you have any question for my problem ....

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to