From 6e2d3acabdd0f4c812bc9a8e92a1173c88f00922 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 25 Apr 2018 17:17:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8Breadmaster?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index b18ee28c..386660c4 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -180,14 +180,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 是否从主库读取数据(主从分布有效) * @access public - * @param bool $master 是否从主库读取 - * @return void + * @param bool $all 是否所有模型生效 + * @return $this */ - public function readMaster($master = true) + public function readMaster($all = false) { - if ($master) { - static::$readMaster[$this->class] = true; - } + $model = $all ? '*' : $this->class; + + static::$readMaster[$model] = true; + return $this; } /** @@ -213,7 +214,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $queryClass = $this->query ?: $con->getConfig('query'); $query = new $queryClass($con, $this); - if (isset(static::$readMaster[$this->class])) { + if (isset(static::$readMaster['*']) || isset(static::$readMaster[$this->class])) { $query->master(true); }