How to connect multiple database in phalcon framework

荒凉一梦 提交于 2020-01-02 07:52:07

问题


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


回答1:


Set your connections in 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" => ""
    ));
});

In your models choose the connection:

public function initialize()
{
    $this->setConnectionService('dbMaster');
    //or
    $this->setConnectionService('dbSlave');
}



回答2:


Another way to do this, using the same configuration

//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" => ""
    ));
});

In your model set

public function initialize()
{
    $this->setReadConnectionService('dbSlave');
    $this->setWriteConnectionService('dbMaster');

    $this->setSource('table_name');
}

If you use transaction, remember in the injector dependency to change

$di->set('dbMaster' ....

For

$di->setShared('dbMaster'....

And the rest is the same



来源:https://stackoverflow.com/questions/22197678/how-to-connect-multiple-database-in-phalcon-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!