使用Zend Framework连接到两个不同的数据库 [英] connecting to two different databases with Zend Framework

查看:226
本文介绍了使用Zend Framework连接到两个不同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个中型内部网站,完全写在Zend FW。内部网的数据库位于另一个服务器上。现在我需要扩展内联网与一些新的功能。为了做到这一点,我需要连接到同一服务器(和相同的DBMS)上的另一个数据库。



现在的问题是:什么是最好的方法?我应该创建一个新的Zend_Config对象和一个新的Zend_Db_Adapter?或者我应该使用现有的,并尝试与使用otherdbname;



还是有更好的方法吗?

一个选项是在 bootstrap.php 中注册两个数据库句柄,每个连接一个。例如:

  $ 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);

在您的控制器(例如):

  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');
...
}


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).

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?

Or is there an even better way to do it?

解决方案

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连接到两个不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆