duanqin9631 2015-10-19 09:24
浏览 8
已采纳

Zend Framework 2中与DB的动态连接

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,

  • 写回答

1条回答 默认 最新

  • duanhuang2804 2015-10-22 10:38
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路