How to connect multiple database in phalcon framew

2019-03-21 20:59发布

问题:

In my code I have two database ABC and XYZ. I want to use both database in same model than What is the solution for it in phalcon? How to implement multiple database connection for this ?

回答1:

one

<?php

//This service returns a MySQL database
$di->set('dbMysql', function() {
     return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "root",
        "password" => "secret",
        "dbname" => "invo"
    ));
});

//This service returns a PostgreSQL database
$di->set('dbPostgres', function() {
     return new \Phalcon\Db\Adapter\Pdo\PostgreSQL(array(
        "host" => "localhost",
        "username" => "postgres",
        "password" => "",
        "dbname" => "invo"
    ));
});

two

<?php

class Robots extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->setConnectionService('dbPostgres');
    }
}

three

<?php

    class Robots extends \Phalcon\Mvc\Model
    {

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

    }

four

class Robots extends Phalcon\Mvc\Model
{
    /**
     * Dynamically selects a shard
     *
     * @param array $intermediate
     * @param array $bindParams
     * @param array $bindTypes
     */
    public function selectReadConnection($intermediate, $bindParams, $bindTypes)
    {
        //Check if there is a 'where' clause in the select
        if (isset($intermediate['where'])) {

            $conditions = $intermediate['where'];

            //Choose the possible shard according to the conditions
            if ($conditions['left']['name'] == 'id') {
                $id = $conditions['right']['value'];
                if ($id > 0 && $id < 10000) {
                    return $this->getDI()->get('dbShard1');
                }
                if ($id > 10000) {
                    return $this->getDI()->get('dbShard2');
                }
            }
        }

        //Use a default shard
        return $this->getDI()->get('dbShard0');
    }

}

five

<?php

$robot = Robots::findFirst('id = 101');


回答2:

You cannot use both database connections in the same model. So:

// Set the connection in the DI
$di->set('database_slave', .....)
$di->set('database_master', .....)

In your model you can only do:

public function initialize()
{
    $this->setConnectionService('database_slave');
}

or

public function initialize()
{
    $this->setConnectionService('database_master');
}

You cannot use both at the same time. What you can do to make your models a bit more flexible is to extend the base model as follows:

class MyModel extends \Phalcon\Mvc\Model
{
    $connection = '';

    public function initialize()
    {
        // Default to the master connection
        $connection = ($this->connection) ? $this->connection : 'database_master';

        $this->setConnectionService($connection);

        parent::initialize()
    }

    public function setMyConnection($connection = 'database_master')
    {
        switch ($connection) {
            case 'database_master':
            case 'database_slave'
                $this->connection = $connection;
                break;
            default:
                $this->connection = 'database_master';
                break;
        }
    }

}

and in your code you can do this

$mymodel = new MyModel();
$mymodel->setMyConnection('database_slave');
// do other stuff - this model will use the slave now.

If you really want to connect to two databases from a model then you can use PHQL and instantiate new objects that connect to different databases within the model. It is not advisable but if that is what you want to do go for it.

Also look at this:

How to connect multiple database in phalcon framework