From 21872be5af28642b0b7ce209e20e98bfcec23ee6 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Apr 2018 14:45:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE=E5=90=8E=E5=8F=8A=E6=97=B6?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 21 +++++++++++++++++++++ library/think/db/Connection.php | 17 ++++++++++++----- library/think/db/Query.php | 27 ++++++++++++++++++++------- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index cbab7155..9c3407d3 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -116,6 +116,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ protected static $initialized = []; + /** + * 是否从主库读取(主从分布式有效) + * @var bool + */ + protected static $readMaster = false; + /** * 构造方法 * @access public @@ -171,6 +177,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->initialize(); } + /** + * 是否从主库读取数据(主从分布有效) + * @access public + * @param bool $master 是否从主库读取 + * @return void + */ + public function readMaster($master) + { + static::$readMaster = $master; + } + /** * 创建模型的查询对象 * @access protected @@ -194,6 +211,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $queryClass = $this->query ?: $con->getConfig('query'); $query = new $queryClass($con, $this); + if (static::$readMaster) { + $query->master(true); + } + // 设置当前数据表和模型名 if (!empty($this->table)) { $query->setTable($this->table); diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index e252b906..731cb49a 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -90,6 +90,8 @@ abstract class Connection 'master_num' => 1, // 指定从服务器序号 'slave_no' => '', + // 模型写入后自动读取主服务器 + 'read_master' => false, // 是否严格检查字段是否存在 'fields_strict' => true, // 数据返回类型 @@ -402,13 +404,14 @@ abstract class Connection /** * 执行语句 * @access public - * @param string $sql sql指令 - * @param array $bind 参数绑定 + * @param string $sql sql指令 + * @param array $bind 参数绑定 + * @param Query $query 查询对象 * @return int * @throws PDOException * @throws \Exception */ - public function execute($sql, $bind = []) + public function execute($sql, $bind = [], Query $query = null) { $this->initConnect(true); if (!$this->linkID) { @@ -447,6 +450,10 @@ abstract class Connection // 调试结束 $this->debug(false); + if ($query && !empty($this->config['deploy']) && !empty($this->config['read_master'])) { + $query->setModelReadMaster(true); + } + $this->numRows = $this->PDOStatement->rowCount(); return $this->numRows; } catch (\PDOException $e) { @@ -744,7 +751,7 @@ abstract class Connection * @param array $sqlArray SQL批处理指令 * @return boolean */ - public function batchQuery($sqlArray = [], $bind = []) + public function batchQuery($sqlArray = [], $bind = [], Query $query = null) { if (!is_array($sqlArray)) { return false; @@ -753,7 +760,7 @@ abstract class Connection $this->startTrans(); try { foreach ($sqlArray as $sql) { - $this->execute($sql, $bind); + $this->execute($sql, $bind, $query); } // 提交事务 $this->commit(); diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 191cc821..f063b07c 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -140,6 +140,19 @@ class Query return $this->model; } + /** + * 设置模型从主库读取数据 + * @access public + * @param bool $master + * @return void + */ + public function setModelReadMaster($master = true) + { + if ($this->model) { + $this->model->readMaster($master); + } + } + /** * 获取当前的builder实例对象 * @access public @@ -238,7 +251,7 @@ class Query */ public function execute($sql, $bind = []) { - return $this->connection->execute($sql, $bind); + return $this->connection->execute($sql, $bind, $this); } /** @@ -2204,7 +2217,7 @@ class Query } // 执行操作 - $result = 0 === $sql ? 0 : $this->execute($sql, $bind); + $result = 0 === $sql ? 0 : $this->execute($sql, $bind, $this); if ($result) { $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); $lastInsId = $this->getLastInsID($sequence); @@ -2270,10 +2283,10 @@ class Query return $this->connection->getRealSql($sql, $bind); } elseif (is_array($sql)) { // 执行操作 - return $this->batchQuery($sql, $bind); + return $this->batchQuery($sql, $bind, $this); } else { // 执行操作 - return $this->execute($sql, $bind); + return $this->execute($sql, $bind, $this); } } @@ -2299,7 +2312,7 @@ class Query return $this->connection->getRealSql($sql, $bind); } else { // 执行操作 - return $this->execute($sql, $bind); + return $this->execute($sql, $bind, $this); } } @@ -2366,7 +2379,7 @@ class Query Cache::clear($options['cache']['tag']); } // 执行操作 - $result = '' == $sql ? 0 : $this->execute($sql, $bind); + $result = '' == $sql ? 0 : $this->execute($sql, $bind, $this); if ($result) { if (is_string($pk) && isset($where[$pk])) { $data[$pk] = $where[$pk]; @@ -2838,7 +2851,7 @@ class Query Cache::clear($options['cache']['tag']); } // 执行操作 - $result = $this->execute($sql, $bind); + $result = $this->execute($sql, $bind, $this); if ($result) { if (!is_array($data) && is_string($pk) && isset($key) && strpos($key, '|')) { list($a, $val) = explode('|', $key);