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); }