如何在Phalcon框架中连接多个数据库 [英] How to connect multiple database in phalcon framework
本文介绍了如何在Phalcon框架中连接多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须连接数据库master和xyz,因为我需要在application中连接两个数据库. 这样就可以在一个应用程序中连接多个数据库,是的,然后怎么做??
i have to database namely master and xyz ,in that i required to connect both database in application . so is it possible to connect multiple database in one application and yes then how.?
推荐答案
在DI中设置连接:
//This service returns a MySQL database
$di->set('dbMaster', function() {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "",
"password" => "",
"dbname" => ""
));
});
//This service returns a PostgreSQL database
$di->set('dbSlave', function() {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "",
"password" => "",
"dbname" => ""
));
});
在模型中选择连接:
public function initialize()
{
$this->setConnectionService('dbMaster');
//or
$this->setConnectionService('dbSlave');
}
这篇关于如何在Phalcon框架中连接多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文