改进Query类和Build类 改进查询语法

This commit is contained in:
thinkphp
2016-04-28 14:06:06 +08:00
parent 006b45dd9a
commit 43fb7cc445
2 changed files with 31 additions and 31 deletions

View File

@@ -282,8 +282,15 @@ abstract class Builder
// 对一个字段使用多个查询条件 // 对一个字段使用多个查询条件
if (is_array($exp)) { 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) { foreach ($val as $item) {
$str[] = $this->parseWhereItem($key, $item); $str[] = $this->parseWhereItem($key, $item, $rule);
} }
return '( ' . implode(' ' . $rule . ' ', $str) . ' )'; return '( ' . implode(' ' . $rule . ' ', $str) . ' )';
} }

View File

@@ -450,19 +450,9 @@ class Query
*/ */
public function where($field, $op = null, $condition = null) public function where($field, $op = null, $condition = null)
{ {
if ($field instanceof Query) { $param = func_get_args();
// 使用查询对象 array_shift($param);
$this->options['where'] = $field; $this->parseWhereExp('AND', $field, $op, $condition, $param);
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);
}
return $this; return $this;
} }
@@ -476,32 +466,32 @@ class Query
*/ */
public function whereOr($field, $op = null, $condition = null) public function whereOr($field, $op = null, $condition = null)
{ {
$where = $this->parseWhereExp($field, $op, $condition); $param = func_get_args();
if (!empty($where)) { array_shift($param);
if (!isset($this->options['where']['OR'])) { $this->parseWhereExp('OR', $field, $op, $condition, $param);
$this->options['where']['OR'] = [];
}
$this->options['where']['OR'] = array_merge($this->options['where']['OR'], $where);
}
return $this; return $this;
} }
/** /**
* 分析查询表达式 * 分析查询表达式
* @access public * @access public
* @param mixed $field 查询字段 * @param string|array|Closure $field 查询字段
* @param mixed $op 查询表达式 * @param mixed $op 查询表达式
* @param mixed $condition 查询条件 * @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'])) { if (is_string($field) && !empty($this->options['via'])) {
$field = $this->options['via'] . '.' . $field; $field = $this->options['via'] . '.' . $field;
} }
if ($field instanceof \Closure) { if (is_null($op) && is_null($condition)) {
$where[] = $field;
} elseif (is_null($op) && is_null($condition)) {
if (is_array($field)) { if (is_array($field)) {
// 数组批量查询 // 数组批量查询
$where = $field; $where = $field;
@@ -512,8 +502,6 @@ class Query
$where = ''; $where = '';
} }
} elseif (is_array($op)) { } elseif (is_array($op)) {
$param = func_get_args();
array_shift($param);
$where[$field] = $param; $where[$field] = $param;
} elseif (in_array(strtolower($op), ['null', 'notnull', 'not null'])) { } elseif (in_array(strtolower($op), ['null', 'notnull', 'not null'])) {
// null查询 // null查询
@@ -524,7 +512,12 @@ class Query
} else { } else {
$where[$field] = [$op, $condition]; $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 = '') public function getOptions($name = '')
{ {
return empty($name) ? $this->options : $this->options[$name]; return isset($this->options[$name]) ? $this->options[$name] : $this->options;
} }
/** /**