简体   繁体   中英

Dynamic connection to DB in Zend Framework 2

I have 3 type of databases:

  • Authen DB (fixed address and fixed schema)

  • Config DB (fixed address and fixed schema)

  • Service DBs (dynamic address and dynamic schema based on each service)

After users logined and verified via Authen DB .

Based on the information store in Config DB , all actions in ZF2 application relate to the service should be done on the correlative Service DBs .

Does ZF2 support this case? How can I solve this?


Below codes are my global.php and local.php.

global.php

return array(
    'db' => array(
        // primary database
        'driver'    => 'Pdo',
        'dsn'       => 'mysql:host=xxx.xxx.xxx.xxx;dbname=db_authen',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ),
        // other database
        'adapters' => array(
            'db_config' => array(
               'driver'         => 'Pdo',
               'dsn'            => 'mysql:host=yyy.yyy.yyy.yyy;dbname=db_config',
               'driver_options' => array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                ),
            ),
        ),
    ),
    'service_manager' => array(
        // primary database
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory'
        ),
        // other database
        'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
        ),
    ),
);

local.php

return array(
    'db' => array(
        // primary database
        'username' => '*****',
        'password' => '*****',

        // other database
        'adapters' => array(
            'db_config' => array(
                'username' => '*****',
                'password' => '*****',
            ),
        ),
    ),
);

Thanks,

After you do your authentication against the (statically configured) Auth DB and consult the (statically configured) Config DB for the dynamic information you need for the Service DB, you could probably instantiate yourself the correct DB-adapter for the Service DB, using something like:

// Config from the Config DB, packaged into an array with keys that 
// are expected by \Zend\Db\Adapter\Adapter
$config = [
    'driver' => 'Pdo_Mysql', // for example
    'user' => 'my-dynamically-obtained-user',
    'password' => 'my-dynamically-obtained-password',
    'database' => 'my-dynamically-obtained-db-name',
    // etc
];
$adapter = new \Zend\Db\Adapter\Adapter($config);

// Now use the $adapter to build queries
$statement = $adapter->query('SELECT * FROM `mytable`');
$results = $statement->execute();

// iterate over the results, etc.

Alternatively, you could feed the $adapter into a model object you create that hides the db-specific query details from the consumer.

See ZF2 docs for \\Zend\\Db\\Adapter\\Adapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM