From 88dfa563b332b862de97d114e16ffc0cda11a013 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 30 Sep 2016 22:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=9A=84=E5=85=A8=E5=B1=80=E5=91=BD=E5=90=8D=E8=8C=83?= =?UTF-8?q?=E5=9B=B4=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 16 ++++++++++------ library/think/model/Relation.php | 3 +++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 692ace0e..8c75fbb2 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1341,9 +1341,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $query = $this->db(); // 全局作用域 - if ($this->useGlobalScope && method_exists($this, 'base')) { - call_user_func_array([$this, 'base'], [ & $query]); - } + $this->baseQuery($query); if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method; @@ -1360,12 +1358,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $query = self::getDb(); $model = new static(); // 全局作用域 - if ($model->useGlobalScope && method_exists($model, 'base')) { - call_user_func_array([$model, 'base'], [ & $query]); - } + $model->baseQuery($query); return call_user_func_array([$query, $method], $params); } + public function baseQuery(&$query) + { + // 全局作用域 + if ($this->useGlobalScope && method_exists($this, 'base')) { + call_user_func_array([$this, 'base'], [ & $query]); + } + } + protected static function getDb() { $model = get_called_class(); diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 22670bdd..6c1b8fca 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -661,6 +661,9 @@ class Relation public function __call($method, $args) { if ($this->query) { + $class = new $this->model; + // 全局作用域 + $class->baseQuery($this->query); switch ($this->type) { case self::HAS_MANY: if (isset($this->where)) {