【问题标题】:How to connect multiple database in phalcon framework at the same time use both in model class not only one如何在phalcon框架中同时连接多个数据库在模型类中不仅使用一个
【发布时间】:2014-03-06 05:22:10
【问题描述】:

在我的代码中,我有两个数据库ABCXYZ。我想在同一模型中使用两个数据库,而不是 phalcon 中的解决方案是什么?如何为此实现多数据库连接?

【问题讨论】:

标签: php database phalcon


【解决方案1】:

一个

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

两个

<?php

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

三个

<?php

    class Robots extends \Phalcon\Mvc\Model
    {

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

    }

四个

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');
    }

}

五个

<?php

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

【讨论】:

  • 如何将其扩展到动态添加的多个分片?您是否应该在服务器上保留一个包含所有分片(JSON、yaml 或 XML)的配置文件?
【解决方案2】:

您不能在同一模型中同时使用两个数据库连接。所以:

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

在你的模型中你只能这样做:

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

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

您不能同时使用两者。为了使模型更加灵活,您可以做的就是扩展基本模型,如下所示:

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;
        }
    }

}

在你的代码中你可以做到这一点

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

如果您真的想从一个模型连接到两个数据库,那么您可以使用PHQL 并实例化连接到模型内不同数据库的新对象。这是不可取的,但如果这是你想做的,那就去吧。

也看看这个:

How to connect multiple database in phalcon framework

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    相关资源
    最近更新 更多