使用Zend框架2进行动态数据库连接?

时间:2022-10-09 17:41:31

How we can develop a rest api with dynamic database connection by checking an existing database in zf2? Now i have developed a rest api with one database. My requirement is the database connection (remote db)should change by usertype after checking the existing database. Database details comes from one main database. How this possible in ZF2? Please help.

我们如何通过检查zf2中的现有数据库来开发具有动态数据库连接的rest api?现在我用一个数据库开发了一个rest api。我的要求是在检查现有数据库后,应使用usertype更改数据库连接(远程数据库)。数据库详细信息来自一个主数据库。在ZF2中这怎么可能?请帮忙。

1 个解决方案

#1


0  

       //Add a service on factories in config/autoload/global.php

      'dynamic-db-adapter' => function($sm) {
       $request = $sm->get('request');
       $key= $request->getPost('key');
     //Master Db Adapter
      $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
  //Make db adapter based on key. Based on key you can fetch db credentials from master db
     $dynamic_adapter = $common->fncGetAdapterObject($dbAdapter, $key);
      return $dynamic_adapter ;
      }

#1


0  

       //Add a service on factories in config/autoload/global.php

      'dynamic-db-adapter' => function($sm) {
       $request = $sm->get('request');
       $key= $request->getPost('key');
     //Master Db Adapter
      $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
  //Make db adapter based on key. Based on key you can fetch db credentials from master db
     $dynamic_adapter = $common->fncGetAdapterObject($dbAdapter, $key);
      return $dynamic_adapter ;
      }