Browse Source

Added support for arbitrary number of parameters for NOT, AND, OR in filter methods of Query

tags/2.0.0-beta
Alexander Makarov 11 years ago
parent
commit
06fdb79730
  1. 21
      extensions/sphinx/Query.php
  2. 21
      framework/db/Query.php
  3. 6
      tests/unit/extensions/sphinx/QueryTest.php
  4. 6
      tests/unit/framework/db/QueryTest.php

21
extensions/sphinx/Query.php

@ -822,11 +822,24 @@ class Query extends Component implements QueryInterface
case 'NOT':
case 'AND':
case 'OR':
$subCondition = $this->filterCondition($condition[1]);
if ($this->parameterNotEmpty($subCondition)) {
$condition[1] = $subCondition;
} else {
for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
$subCondition = $this->filterCondition($condition[$i]);
if ($this->parameterNotEmpty($subCondition)) {
$condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
}
break;
case 'IN':

21
framework/db/Query.php

@ -909,11 +909,24 @@ class Query extends Component implements QueryInterface
case 'NOT':
case 'AND':
case 'OR':
$subCondition = $this->filterCondition($condition[1]);
if ($this->parameterNotEmpty($subCondition)) {
$condition[1] = $subCondition;
} else {
for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
$subCondition = $this->filterCondition($condition[$i]);
if ($this->parameterNotEmpty($subCondition)) {
$condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
}
break;
case 'IN':

6
tests/unit/extensions/sphinx/QueryTest.php

@ -120,11 +120,7 @@ class QueryTest extends SphinxTestCase
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', '']]);
$query->filter(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
$this->assertEquals(['id' => 1], $query->where);
}

6
tests/unit/framework/db/QueryTest.php

@ -109,11 +109,7 @@ class QueryTest extends DatabaseTestCase
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', '']]);
$query->filter(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
$this->assertEquals(['id' => 1], $query->where);
}

Loading…
Cancel
Save