From f3e634f1b3e61f4d50340c1f43da5e0dd025978a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 14 Aug 2016 09:48:24 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0useGlobalScope?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=AE=BE=E7=BD=AE=E6=98=AF=E5=90=A6=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E5=85=A8=E5=B1=80=E6=9F=A5=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 | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index d09322c4..8e460ee9 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -95,6 +95,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $relation; // 验证失败是否抛出异常 protected $failException = false; + // 全局查询范围 + protected static $useGlobalScope = true; /** * 初始化过的模型. @@ -1079,6 +1081,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $model; } + /** + * 设置是否使用全局查询范围 + * @param bool $use 是否启用全局查询范围 + * @access public + * @return Model + */ + public static function useGlobalScope($use) + { + $model = new static(); + static::$useGlobalScope = $use; + return $model; + } + /** * 根据关联条件查询当前模型 * @access public @@ -1299,7 +1314,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $query = $this->db(); // 全局作用域 - if (method_exists($this, 'base')) { + if (static::$useGlobalScope && method_exists($this, 'base')) { call_user_func_array('static::base', [ & $query]); } if (method_exists($this, 'scope' . $method)) { @@ -1321,7 +1336,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $query = self::$links[$model]; // 全局作用域 - if (method_exists($model, 'base')) { + if (static::$useGlobalScope && method_exists($model, 'base')) { call_user_func_array('static::base', [ & $query]); } return call_user_func_array([$query, $method], $params);