From a9782f094a9167996ac81d0f3932d69bb783c485 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Oct 2016 15:32:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=A8=E5=B1=80=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 20 ++++++-------------- library/think/model/Relation.php | 3 --- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 8c75fbb2..f062e3c5 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -147,9 +147,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 获取当前模型的数据库查询对象 * @access public + * @param bool $baseQuery 是否调用全局查询范围 * @return Query */ - public function db() + public function db($baseQuery = true) { $model = $this->class; if (!isset(self::$links[$model])) { @@ -169,6 +170,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess self::$links[$model] = $query; } + // 全局作用域 + if ($baseQuery && method_exists($this, 'base')) { + call_user_func_array([$this, 'base'], [ & self::$links[$model]]); + } // 返回当前模型的数据库查询对象 return self::$links[$model]; } @@ -1340,8 +1345,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function __call($method, $args) { $query = $this->db(); - // 全局作用域 - $this->baseQuery($query); if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method; @@ -1356,20 +1359,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function __callStatic($method, $params) { $query = self::getDb(); - $model = new static(); - // 全局作用域 - $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 6c1b8fca..22670bdd 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -661,9 +661,6 @@ 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)) {