From 625a8767c5e29398f5e945fdb6bab02a8ca6e9ca Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 2 Jun 2016 14:18:18 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E6=9F=A5=E8=AF=A2=E8=8C=83=E5=9B=B4=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=20=E6=94=B9=E8=BF=9BQuery=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 5 ++++- library/think/db/Query.php | 16 +++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index e1f9a7a0..4a7cc7e6 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -964,7 +964,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } else { $query->name($this->name); } - + // 全局作用域 + if (method_exists($this, 'base')) { + call_user_func_array([$this, 'base'], [ & $query]); + } self::$links[$model] = $query; } // 返回当前模型的数据库查询对象 diff --git a/library/think/db/Query.php b/library/think/db/Query.php index f2625335..1f61f856 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1437,7 +1437,6 @@ class Query } else { $where[$key] = strpos($data, ',') ? ['IN', $data] : $data; } - $options['where']['AND'] = $where; } elseif (is_array($pk) && is_array($data) && !empty($data)) { // 根据复合主键查询 foreach ($pk as $key) { @@ -1448,7 +1447,14 @@ class Query throw new Exception('miss complex primary data'); } } - $options['where']['AND'] = $where; + } + + if (!empty($where)) { + if (isset($options['where']['AND'])) { + $options['where']['AND'] = array_merge($options['where']['AND'], $where); + } else { + $options['where']['AND'] = $where; + } } return; } @@ -1590,7 +1596,7 @@ class Query if (false === $data) { // 用于子查询 不查询只返回SQL $options['fetch_sql'] = true; - } elseif (empty($options['where']) && !empty($data)) { + } elseif (!empty($data)) { // 主键条件分析 $this->parsePkWhere($data, $options); } @@ -1670,7 +1676,7 @@ class Query // 分析查询表达式 $options = $this->parseExpress(); - if (empty($options['where']) && (!empty($data) || 0 == $data)) { + if (!empty($data) || 0 == $data) { // AR模式分析主键条件 $this->parsePkWhere($data, $options); } @@ -1798,7 +1804,7 @@ class Query // 分析查询表达式 $options = $this->parseExpress(); - if (empty($options['where']) && !empty($data)) { + if (!empty($data)) { // AR模式分析主键条件 $this->parsePkWhere($data, $options); }