gpt4 book ai didi

phalcon - 如何在phalcon框架中连接多个数据库

转载 作者:行者123 更新时间:2023-12-04 23:42:46 27 4
gpt4 key购买 nike

我必须建立数据库,即 master 和 xyz,因为我需要在应用程序中连接这两个数据库。
那么是否可以在一个应用程序中连接多个数据库,是的,那么如何。?

最佳答案

在 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 - 如何在phalcon框架中连接多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22197678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com