From 10aae58d822ad8182b870d5c88e52af8b4a246b0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 6 Aug 2016 14:49:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=BD=AF=E5=88=A0=E9=99=A4tr?= =?UTF-8?q?ait=20Model=E7=B1=BB=E7=9A=84base=E6=96=B9=E6=B3=95=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E9=9D=99=E6=80=81=E5=AE=9A=E4=B9=89=20=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=A4=9A=E6=AC=A1=E8=B0=83=E7=94=A8=E6=9C=89=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 17 +++++---- library/traits/model/SoftDelete.php | 56 ++++------------------------- 2 files changed, 18 insertions(+), 55 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 12a7e0e2..a870f347 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -166,10 +166,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $query->pk($this->pk); } - // 全局作用域 - if (method_exists($this, 'base')) { - call_user_func_array([$this, 'base'], [ & $query]); - } self::$links[$model] = $query; } // 返回当前模型的数据库查询对象 @@ -1290,14 +1286,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function __call($method, $args) { + $query = $this->db(); + // 全局作用域 + if (method_exists($this, 'base')) { + call_user_func_array('static::base', [ & $query]); + } if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 $method = 'scope' . $method; - array_unshift($args, $this->db()); + array_unshift($args, $query); call_user_func_array([$this, $method], $args); return $this; } else { - return call_user_func_array([$this->db(), $method], $args); + return call_user_func_array([$query, $method], $args); } } @@ -1308,6 +1309,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess self::$links[$model] = (new static())->db(); } $query = self::$links[$model]; + // 全局作用域 + if (method_exists($model, 'base')) { + call_user_func_array('static::base', [ & $query]); + } return call_user_func_array([$query, $method], $params); } diff --git a/library/traits/model/SoftDelete.php b/library/traits/model/SoftDelete.php index 854a55fe..f854a307 100644 --- a/library/traits/model/SoftDelete.php +++ b/library/traits/model/SoftDelete.php @@ -4,35 +4,6 @@ namespace traits\model; trait SoftDelete { - /** - * 分析查询表达式 - * @access public - * @param mixed $data 主键列表或者查询条件(闭包) - * @param string $with 关联预查询 - * @param bool $cache 是否缓存 - * @return Query - */ - protected static function parseQuery(&$data, $with, $cache) - { - $result = self::with($with)->cache($cache); - if (is_array($data) && key($data) !== 0) { - $result = $result->where($data); - $data = null; - } elseif ($data instanceof \Closure) { - call_user_func_array($data, [ & $result]); - $data = null; - } elseif ($data instanceof Query) { - $result = $data->with($with)->cache($cache); - $data = null; - } - - if (static::$deleteTime) { - // 默认不查询软删除数据 - $result->where(static::$deleteTime, 0); - } - return $result; - } - /** * 查询软删除数据 * @access public @@ -96,29 +67,16 @@ trait SoftDelete return false; } - public function __call($method, $args) + /** + * 查询默认不包含软删除数据 + * @access protected + * @return void + */ + protected static function base($query) { - if (method_exists($this, 'scope' . $method)) { - // 动态调用命名范围 - $method = 'scope' . $method; - array_unshift($args, $this->db()); - call_user_func_array([$this, $method], $args); - return $this; - } else { - $query = $this->db(); + if (static::$deleteTime) { $query->where(static::$deleteTime, 0); - return call_user_func_array([$this->db(), $method], $args); } } - public static function __callStatic($method, $params) - { - $model = get_called_class(); - if (!isset(self::$links[$model])) { - self::$links[$model] = (new static())->db(); - } - $query = self::$links[$model]; - $query->where(static::$deleteTime, 0); - return call_user_func_array([$query, $method], $params); - } }