• <i id='nhqZT'><tr id='nhqZT'><dt id='nhqZT'><q id='nhqZT'><span id='nhqZT'><b id='nhqZT'><form id='nhqZT'><ins id='nhqZT'></ins><ul id='nhqZT'></ul><sub id='nhqZT'></sub></form><legend id='nhqZT'></legend><bdo id='nhqZT'><pre id='nhqZT'><center id='nhqZT'></center></pre></bdo></b><th id='nhqZT'></th></span></q></dt></tr></i><div id='nhqZT'><tfoot id='nhqZT'></tfoot><dl id='nhqZT'><fieldset id='nhqZT'></fieldset></dl></div>

      • <bdo id='nhqZT'></bdo><ul id='nhqZT'></ul>
    1. <small id='nhqZT'></small><noframes id='nhqZT'>

        <legend id='nhqZT'><style id='nhqZT'><dir id='nhqZT'><q id='nhqZT'></q></dir></style></legend>

        <tfoot id='nhqZT'></tfoot>

        使用 Zend Framework 连接到两个不同的数据库

        时间:2023-10-02

        <small id='XvkHJ'></small><noframes id='XvkHJ'>

          <bdo id='XvkHJ'></bdo><ul id='XvkHJ'></ul>
              <legend id='XvkHJ'><style id='XvkHJ'><dir id='XvkHJ'><q id='XvkHJ'></q></dir></style></legend>
                <tbody id='XvkHJ'></tbody>

              • <i id='XvkHJ'><tr id='XvkHJ'><dt id='XvkHJ'><q id='XvkHJ'><span id='XvkHJ'><b id='XvkHJ'><form id='XvkHJ'><ins id='XvkHJ'></ins><ul id='XvkHJ'></ul><sub id='XvkHJ'></sub></form><legend id='XvkHJ'></legend><bdo id='XvkHJ'><pre id='XvkHJ'><center id='XvkHJ'></center></pre></bdo></b><th id='XvkHJ'></th></span></q></dt></tr></i><div id='XvkHJ'><tfoot id='XvkHJ'></tfoot><dl id='XvkHJ'><fieldset id='XvkHJ'></fieldset></dl></div>
                  <tfoot id='XvkHJ'></tfoot>
                  本文介绍了使用 Zend Framework 连接到两个不同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我这里有一个完全用 Zend FW 编写的中型 Intranet 站点.Intranet 的数据库位于另一台服务器上.现在我需要使用一些新功能来扩展 Intranet.为了做到这一点,我需要连接到同一台服务器(和同一个 DBMS)上的另一个数据库.

                  I have here a medium sized intranet site which is written entirely in Zend FW. The database for the intranet is located on another server. Now I need to extend the intranet with some new functionality. In order to do this I need to connect to another database on the same server (and same DBMS).

                  现在的问题是:这样做的最佳方法是什么?我应该创建一个新的 Zend_Config 对象和一个新的 Zend_Db_Adapter 吗?或者我应该使用现有的并尝试使用use otherdbname;"在同一会话中连接到新数据库的语句?

                  The question is now: What is the best way to do this? Should I create a new Zend_Config object and a new Zend_Db_Adapter? Or should I use the existing one and try with the "use otherdbname;" statement to connect within the same session to the new database?

                  或者有更好的方法吗?

                  推荐答案

                  一种选择是从您的 bootstrap.php 中注册 2 个数据库句柄,每个连接一个.例如:

                  One option is to register 2 database handles from within your bootstrap.php, one for each connection. E.g.:

                  $parameters = array(
                                      'host'     => 'xx.xxx.xxx.xxx',
                                      'username' => 'test',
                                      'password' => 'test',
                                      'dbname'   => 'test'
                                     );
                  try {
                      $db = Zend_Db::factory('Pdo_Mysql', $parameters);
                      $db->getConnection();
                  } catch (Zend_Db_Adapter_Exception $e) {
                      echo $e->getMessage();
                      die('Could not connect to database.');
                  } catch (Zend_Exception $e) {
                      echo $e->getMessage();
                      die('Could not connect to database.');
                  }
                  Zend_Registry::set('db', $db);
                  
                  $parameters = array(
                                      'host'     => 'xx.xxx.xxx.xxx',
                                      'username' => 'test',
                                      'password' => 'test',
                                      'dbname'   => 'test'
                                     );
                  try {
                      $db = Zend_Db::factory('Pdo_Mysql', $parameters);
                      $db->getConnection();
                  } catch (Zend_Db_Adapter_Exception $e) {
                      echo $e->getMessage();
                      die('Could not connect to database.');
                  } catch (Zend_Exception $e) {
                      echo $e->getMessage();
                      die('Could not connect to database.');
                  }
                  Zend_Registry::set('db2', $db);
                  

                  在您的控制器中(例如):

                  In your controllers (e.g.):

                  public function init()
                  {
                       $this->db = Zend_Registry::get('db');
                       $this->db2 = Zend_Registry::get('db2');
                  }
                  
                  public function fooAction()
                  {
                      $data = $this->db2->fetchAll('select foo from blah');
                      ...
                  }
                  

                  这篇关于使用 Zend Framework 连接到两个不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Zend Framework 2 - 如何使用外部库 下一篇:Zend 框架 Oauth 提供程序

                  相关文章

                  最新文章

                • <tfoot id='67LYR'></tfoot>
                • <legend id='67LYR'><style id='67LYR'><dir id='67LYR'><q id='67LYR'></q></dir></style></legend>
                      <bdo id='67LYR'></bdo><ul id='67LYR'></ul>
                    <i id='67LYR'><tr id='67LYR'><dt id='67LYR'><q id='67LYR'><span id='67LYR'><b id='67LYR'><form id='67LYR'><ins id='67LYR'></ins><ul id='67LYR'></ul><sub id='67LYR'></sub></form><legend id='67LYR'></legend><bdo id='67LYR'><pre id='67LYR'><center id='67LYR'></center></pre></bdo></b><th id='67LYR'></th></span></q></dt></tr></i><div id='67LYR'><tfoot id='67LYR'></tfoot><dl id='67LYR'><fieldset id='67LYR'></fieldset></dl></div>

                    <small id='67LYR'></small><noframes id='67LYR'>