mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-16 15:21:13 +08:00
Query::filter() adjustments
This commit is contained in:
@ -92,7 +92,7 @@ class Query extends Component implements QueryInterface
|
||||
* @var array|string The filter part of this search query. This is an array or json string that follows the format of
|
||||
* the elasticsearch [Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html).
|
||||
*/
|
||||
public $filter;
|
||||
public $filterPart;
|
||||
|
||||
public $facets = [];
|
||||
|
||||
@ -459,9 +459,9 @@ class Query extends Component implements QueryInterface
|
||||
* @param string $filter
|
||||
* @return static the query object itself
|
||||
*/
|
||||
public function applyFilter($filter)
|
||||
public function filterPart($filter)
|
||||
{
|
||||
$this->filter = $filter;
|
||||
$this->filterPart = $filter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -62,17 +62,17 @@ class QueryBuilder extends \yii\base\Object
|
||||
}
|
||||
|
||||
$whereFilter = $this->buildCondition($query->where);
|
||||
if (is_string($query->filter)) {
|
||||
if (is_string($query->filterPart)) {
|
||||
if (empty($whereFilter)) {
|
||||
$parts['filter'] = $query->filter;
|
||||
$parts['filter'] = $query->filterPart;
|
||||
} else {
|
||||
$parts['filter'] = '{"and": [' . $query->filter . ', ' . Json::encode($whereFilter) . ']}';
|
||||
$parts['filter'] = '{"and": [' . $query->filterPart . ', ' . Json::encode($whereFilter) . ']}';
|
||||
}
|
||||
} elseif ($query->filter !== null) {
|
||||
} elseif ($query->filterPart !== null) {
|
||||
if (empty($whereFilter)) {
|
||||
$parts['filter'] = $query->filter;
|
||||
$parts['filter'] = $query->filterPart;
|
||||
} else {
|
||||
$parts['filter'] = ['and' => [$query->filter, $whereFilter]];
|
||||
$parts['filter'] = ['and' => [$query->filterPart, $whereFilter]];
|
||||
}
|
||||
} elseif (!empty($whereFilter)) {
|
||||
$parts['filter'] = $whereFilter;
|
||||
|
@ -819,6 +819,16 @@ class Query extends Component implements QueryInterface
|
||||
$operator = strtoupper($condition[0]);
|
||||
|
||||
switch ($operator) {
|
||||
case 'NOT':
|
||||
case 'AND':
|
||||
case 'OR':
|
||||
$subCondition = $this->filterCondition($condition[1]);
|
||||
if ($this->parameterNotEmpty($subCondition)) {
|
||||
$condition[1] = $subCondition;
|
||||
} else {
|
||||
$condition = [];
|
||||
}
|
||||
break;
|
||||
case 'IN':
|
||||
case 'NOT IN':
|
||||
case 'LIKE':
|
||||
@ -837,7 +847,7 @@ class Query extends Component implements QueryInterface
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$condition = $this->filterConditionHash($condition);
|
||||
$condition = $this->filterHashCondition($condition);
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
|
@ -906,6 +906,16 @@ class Query extends Component implements QueryInterface
|
||||
$operator = strtoupper($condition[0]);
|
||||
|
||||
switch ($operator) {
|
||||
case 'NOT':
|
||||
case 'AND':
|
||||
case 'OR':
|
||||
$subCondition = $this->filterCondition($condition[1]);
|
||||
if ($this->parameterNotEmpty($subCondition)) {
|
||||
$condition[1] = $subCondition;
|
||||
} else {
|
||||
$condition = [];
|
||||
}
|
||||
break;
|
||||
case 'IN':
|
||||
case 'NOT IN':
|
||||
case 'LIKE':
|
||||
@ -924,7 +934,7 @@ class Query extends Component implements QueryInterface
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$condition = $this->filterConditionHash($condition);
|
||||
$condition = $this->filterHashCondition($condition);
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ trait QueryTrait
|
||||
* @param array $condition original condition
|
||||
* @return array condition with empty parameters removed
|
||||
*/
|
||||
protected function filterConditionHash($condition)
|
||||
protected function filterHashCondition($condition)
|
||||
{
|
||||
if (is_array($condition) && !isset($condition[0])) {
|
||||
// hash format: 'column1' => 'value1', 'column2' => 'value2', ...
|
||||
@ -126,7 +126,7 @@ trait QueryTrait
|
||||
*/
|
||||
protected function filterCondition($condition)
|
||||
{
|
||||
return $this->filterConditionHash($condition);
|
||||
return $this->filterHashCondition($condition);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -117,6 +117,17 @@ class QueryTest extends SphinxTestCase
|
||||
$this->assertEquals($condition, $query->where);
|
||||
}
|
||||
|
||||
public function testFilterRecursively()
|
||||
{
|
||||
$query = new Query();
|
||||
$query->filter(['not', ['like', 'name', '']]);
|
||||
$this->assertEquals(null, $query->where);
|
||||
|
||||
$query->where(['id' => 1]);
|
||||
$query->filter(['and', ['like', 'name', '']]);
|
||||
$this->assertEquals(['id' => 1], $query->where);
|
||||
}
|
||||
|
||||
public function testGroup()
|
||||
{
|
||||
$query = new Query;
|
||||
|
@ -106,6 +106,17 @@ class QueryTest extends DatabaseTestCase
|
||||
$this->assertEquals($condition, $query->where);
|
||||
}
|
||||
|
||||
public function testFilterRecursively()
|
||||
{
|
||||
$query = new Query();
|
||||
$query->filter(['not', ['like', 'name', '']]);
|
||||
$this->assertEquals(null, $query->where);
|
||||
|
||||
$query->where(['id' => 1]);
|
||||
$query->filter(['and', ['like', 'name', '']]);
|
||||
$this->assertEquals(['id' => 1], $query->where);
|
||||
}
|
||||
|
||||
public function testJoin()
|
||||
{
|
||||
}
|
||||
|
Reference in New Issue
Block a user