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)) {