BUG: exclude() clears previously selected filters() (Trac #7529)

This bug was caused by the fact that SQLQuery::whereAny() removed existing filters.  In line with addWhere() and setWhere(), I split this into addWhereAny() and setWhereAny().  Strictly speaking, this drops the method SQLQuery::whereAny(), but it was really just an internal function for exclude, and so I think that's acceptable.
This commit is contained in:
Sam Minnee 2012-06-22 14:34:06 +12:00
parent bafda93957
commit 1b5768975c
4 changed files with 29 additions and 4 deletions

View File

@ -461,7 +461,7 @@ function max($field) {
function whereAny($filter) {
if($filter) {
$clone = $this;
$clone->query->whereAny($filter);
$clone->query->addWhereAny($filter);
return $clone;
} else {
return $this;

View File

@ -738,14 +738,28 @@ class SQLQuery {
return $this->setWhere($where);
}
public function whereAny($where) {
Deprecation::notice('3.0', 'Please use setWhereAny() or setWhereAny() instead!');
return $this->setWhereAny($where);
}
/**
* @param String|array $filters Predicate(s) to set, as escaped SQL statements.
*/
function whereAny($filters) {
function setWhereAny($filters) {
if(is_string($filters)) $filters = func_get_args();
$clause = implode(" OR ", $filters);
return $this->setWhere($clause);
}
/**
* @param String|array $filters Predicate(s) to set, as escaped SQL statements.
*/
function addWhereAny($filters) {
if(is_string($filters)) $filters = func_get_args();
$clause = implode(" OR ", $filters);
return $this->addWhere($clause);
}
/**
* Use the disjunctive operator 'OR' to join filter expressions in the WHERE clause.

View File

@ -437,6 +437,17 @@ class DataListTest extends SapphireTest {
$list->exclude(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob'));
$this->assertEquals(2, $list->count());
}
/**
* Test that if an exclude() is applied to a filter(), the filter() is still preserved.
*/
public function testExcludeOnFilter() {
$list = DataObjectTest_TeamComment::get();
$list = $list->filter('Comment', 'Phil is a unique guy, and comments on team2');
$list = $list->exclude('Name', 'Bob');
$this->assertContains('WHERE ("Comment" = \'Phil is a unique guy, and comments on team2\') AND ("Name" != \'Bob\')', $list->sql());
}
/**
* $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43

View File

@ -258,11 +258,11 @@ class SQLQueryTest extends SapphireTest {
);
}
public function testWhereAny() {
public function testSetWhereAny() {
$query = new SQLQuery();
$query->setFrom('MyTable');
$query->whereAny(array("Monkey = 'Chimp'", "Color = 'Brown'"));
$query->setWhereAny(array("Monkey = 'Chimp'", "Color = 'Brown'"));
$this->assertEquals("SELECT * FROM MyTable WHERE (Monkey = 'Chimp' OR Color = 'Brown')",$query->sql());
}