zend 2: Organizing my database adapters in autoload config

I am writing an application that unfortunately must utilize multiple DBs. I would like to organize the adapters in my config\autoload\global.php file like this: (the code below does not work)

'appDomain1' => array(
    'Db1' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db1;host=myserver',
        'username' => 'red',
        'password' => '123456',
    ),
    'Db2' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db2;host=myserver',
        'username' => 'red',
        'password' => '123456',
    ),
    'Db3' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db3;host=myserver',
        'username' => 'red',
        'password' => '123456',
    ),
),
'appDomain2' => array(
    'Db5' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db5;host=myserver',
        'username' => 'red',
        'password' => '123456',
    ),
    'Db6' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db6;host=myserver',
        'username' => 'red',
        'password' => '123456',
    ),
    'Db7' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db7;host=myserver',
        'username' => 'red',
        'password' => '123456',
    ),
),

'db' => array(
    //Default adapter
    'driver' => 'Pdo_Mysql',
    'dsn' => 'mysql:dbname=defaultDB;host=myserver',
    'username' => 'red',
    'password' => '123456',
    //'driver_options' => array(
    //  PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    //),
    'adapters' => array(
        'appDomain1',
        'appDomain2',
    ),
),

So you can see how I would like to logically group my database adapters and then just pass them to the 'adapters' array in the 'db' config. This would be advantageous because I could then refer to my adapters via their logical grouping in the service manager config (to loop through them) like this:

$config = $this->getServiceLocator()->get('config');
$appDomain1 = $config['appDomain1'];

I can't just pass them in like in my code above though because the adapters array is not expecting nested arrays like my 'appDomain1' and 'appDomain2' arrays.

How can I logically group my adapters like this?

edit: I came up with the following solution, but not sure if what I'm doing is bad. I found I could just throw my own custom key in with the nested arrays in the adapters array and use that (is this a bad idea?).

Right now I'm doing the following in my global.php\local.php under autoload:

'db' => array(
    //Default adapter
    'driver' => 'Pdo_Mysql',
    'dsn' => 'mysql:dbname=defaultDB;host=myserver',
    'username' => 'red',
    'password' => '123456',
    //'driver_options' => array(
    //  PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    //),
    'adapters' => array(
    'Db5' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db5;host=myserver',
        'username' => 'red',
        'password' => '123456',
        'myAppType' => 'appDomain1',
    ),
    'Db6' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db6;host=myserver',
        'username' => 'red',
        'password' => '123456',
        'myAppType' => 'appDomain2',
    ),
    'Db7' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=db7;host=myserver',
        'username' => 'red',
        'password' => '123456',
        'myAppType' => 'appDomain3',
    ),
    ),
),

Now I can pickup that custom 'myAppType' => 'appDomainX' key I added like this:

$dbAdapters = $this->getServiceLocator()->get('config')['db']['adapters'];
foreach ($dbAdapters as $dbAdapter) {
    if (array_key_exists('myAppType', $dbAdapter)) {
        if ($dbAdapter['myAppType'] === 'appDomain2' ) {
            var_dump($dbAdapter);
        }
    }
}

Is this a workable solution? Seems fine to me, but I want to make sure this isn't a nono.

Answers


There is nothing wrong with what you're doing but it it less practical then your previous setup. You can however write your own Adapter Factory and use your old structure. It is not that hard because you can use almost all the code that is in the standard Adapter Factory.


Need Your Help

Scoping issue with a helper method to manipulate a string

c++ string scope

I want to use a helper method to perform an operation on a string. I would like the method to have two parameters: input and output. I think there is a scoping problem with how I want to solve the