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

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图