假设你已经在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');
    }
}

相关文章:

  • 2022-12-23
  • 2023-03-25
  • 2021-05-23
  • 2021-05-20
  • 2021-07-30
  • 2021-07-23
  • 2021-05-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2022-01-26
  • 2021-10-12
相关资源
相似解决方案