码迷,mamicode.com
首页 > 其他好文 > 详细

Phalcon框架如何实现读写分离

时间:2015-05-17 00:49:37      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:

Phalcon框架如何实现读写分离

假设你已经在DI容器里注册了俩 db services,如下:

<?php
// 主库
$di->setShared(‘dbWrite‘, function() use ($config) {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => $config->w_database->host,
        "username" => $config->w_database->username,
        "password" => $config->w_database->password,
        "dbname" => $config->w_database->name
    ));
});
//  从库VIP
$di->setShared(‘dbRead‘, function() use ($config) {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => $config->r_database->host,
        "username" => $config->r_database->username,
        "password" => $config->r_database->password,
        "dbname" =>  $config->r_database->name
    ));
});

然后在 Model 中这么处理就可以了:

<?php
class UserModel extends \Phalcon\Mvc\Model {
    public function initialize() {
        parent::initialize();
        $this->setReadConnectionService(‘dbRead‘);
        $this->setWriteConnectionService(‘dbWrite‘);
    }
}

Phalcon框架如何实现读写分离

标签:

原文地址:http://www.cnblogs.com/sandea/p/4508977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!