4

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 ?

Rikesh
  • 26,156
  • 14
  • 79
  • 87
radhika
  • 89
  • 2
  • 4
  • http://docs.phalconphp.com/en/latest/reference/di.html Just create several connections, and use them as described. – Eternal1 Mar 06 '14 at 06:16
  • possible duplicate of :- http://stackoverflow.com/questions/22197678/how-to-connect-multiple-database-in-phalcon-framework – Jagjot Mar 06 '14 at 10:36
  • @Parry In your Link it's master slave concept.I am asking about parallel connection. – radhika Mar 06 '14 at 12:43

2 Answers2

13

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');
宋小北
  • 151
  • 1
  • 6
  • How to scale this to a several shards added dinamically? Should you keep a configuration file containing all shards on the server (JSON, yaml or XML)? – tonix Oct 13 '20 at 10:57
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

Community
  • 1
  • 1
Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67