1
我必須數據庫即master和xyz,因爲我需要在應用程序中連接兩個數據庫。 所以有可能在一個應用程序中連接多個數據庫,然後是如何。如何在phalcon框架中連接多個數據庫
我必須數據庫即master和xyz,因爲我需要在應用程序中連接兩個數據庫。 所以有可能在一個應用程序中連接多個數據庫,然後是如何。如何在phalcon框架中連接多個數據庫
設置你的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');
}
另一種方式來做到這一點,使用相同的配置
//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->setReadConnectionService('dbSlave');
$this->setWriteConnectionService('dbMaster');
$this->setSource('table_name');
}
如果你使用事務,記得在噴油器的依賴性改變
$di->set('dbMaster' ....
對於
$di->setShared('dbMaster'....
,剩下的就是同
但使用這種編碼,我可以只使用一個數據庫在模型中的時間,但我想在同一模型中使用兩個數據庫 –
我很抱歉,但我不知道你試圖達到什麼。可能你有你的理由,但作爲建議,你應該嘗試保持模型,一個連接和一個表。如果你能更好地解釋你的目標是什麼,也許我或其他人可以用正確的解決方案來幫助你。 – crada