From 43fb7cc4450d59a2d8a496fac85a2b2c0c6524c5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 28 Apr 2016 14:06:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E5=92=8CBuild?= =?UTF-8?q?=E7=B1=BB=20=E6=94=B9=E8=BF=9B=E6=9F=A5=E8=AF=A2=E8=AF=AD?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 9 +++++- library/think/db/Query.php | 53 ++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index ccdb301c..d579e9b9 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -282,8 +282,15 @@ abstract class Builder // 对一个字段使用多个查询条件 if (is_array($exp)) { + $item = array_pop($val); + // 传入 or 或者 and + if (is_string($item) && in_array($item, ['AND', 'and', 'OR', 'or'])) { + $rule = $item; + } else { + array_push($val, $item); + } foreach ($val as $item) { - $str[] = $this->parseWhereItem($key, $item); + $str[] = $this->parseWhereItem($key, $item, $rule); } return '( ' . implode(' ' . $rule . ' ', $str) . ' )'; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index a9ba0a5d..b54e84d4 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -450,19 +450,9 @@ class Query */ public function where($field, $op = null, $condition = null) { - if ($field instanceof Query) { - // 使用查询对象 - $this->options['where'] = $field; - return $this; - } - - $where = $this->parseWhereExp($field, $op, $condition); - if (!empty($where)) { - if (!isset($this->options['where']['AND'])) { - $this->options['where']['AND'] = []; - } - $this->options['where']['AND'] = array_merge($this->options['where']['AND'], $where); - } + $param = func_get_args(); + array_shift($param); + $this->parseWhereExp('AND', $field, $op, $condition, $param); return $this; } @@ -476,32 +466,32 @@ class Query */ public function whereOr($field, $op = null, $condition = null) { - $where = $this->parseWhereExp($field, $op, $condition); - if (!empty($where)) { - if (!isset($this->options['where']['OR'])) { - $this->options['where']['OR'] = []; - } - $this->options['where']['OR'] = array_merge($this->options['where']['OR'], $where); - } + $param = func_get_args(); + array_shift($param); + $this->parseWhereExp('OR', $field, $op, $condition, $param); return $this; } /** * 分析查询表达式 * @access public - * @param mixed $field 查询字段 + * @param string|array|Closure $field 查询字段 * @param mixed $op 查询表达式 * @param mixed $condition 查询条件 - * @return $this + * @param string $operator and or + * @return void */ - protected function parseWhereExp($field, $op, $condition) + protected function parseWhereExp($operator, $field, $op, $condition, $param = []) { + if ($field instanceof \Closure) { + call_user_func_array($field, [ & $this]); + return; + } + if (is_string($field) && !empty($this->options['via'])) { $field = $this->options['via'] . '.' . $field; } - if ($field instanceof \Closure) { - $where[] = $field; - } elseif (is_null($op) && is_null($condition)) { + if (is_null($op) && is_null($condition)) { if (is_array($field)) { // 数组批量查询 $where = $field; @@ -512,8 +502,6 @@ class Query $where = ''; } } elseif (is_array($op)) { - $param = func_get_args(); - array_shift($param); $where[$field] = $param; } elseif (in_array(strtolower($op), ['null', 'notnull', 'not null'])) { // null查询 @@ -524,7 +512,12 @@ class Query } else { $where[$field] = [$op, $condition]; } - return $where; + if (!empty($where)) { + if (!isset($this->options['where'][$operator])) { + $this->options['where'][$operator] = []; + } + $this->options['where'][$operator] = array_merge($this->options['where'][$operator], $where); + } } /** @@ -965,7 +958,7 @@ class Query public function getOptions($name = '') { - return empty($name) ? $this->options : $this->options[$name]; + return isset($this->options[$name]) ? $this->options[$name] : $this->options; } /**