From 1b5768975c24014f5a9cd74cdf35a6ec3716ac3d Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 22 Jun 2012 14:34:06 +1200 Subject: [PATCH] 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. --- model/DataQuery.php | 2 +- model/SQLQuery.php | 16 +++++++++++++++- tests/model/DataListTest.php | 11 +++++++++++ tests/model/SQLQueryTest.php | 4 ++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/model/DataQuery.php b/model/DataQuery.php index 8d2a4be36..e220961a8 100644 --- a/model/DataQuery.php +++ b/model/DataQuery.php @@ -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; diff --git a/model/SQLQuery.php b/model/SQLQuery.php index 3808fecbd..daf9e0ad0 100644 --- a/model/SQLQuery.php +++ b/model/SQLQuery.php @@ -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. diff --git a/tests/model/DataListTest.php b/tests/model/DataListTest.php index 232631061..ff2b9edb6 100755 --- a/tests/model/DataListTest.php +++ b/tests/model/DataListTest.php @@ -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 diff --git a/tests/model/SQLQueryTest.php b/tests/model/SQLQueryTest.php index e23b5022b..3819b352c 100755 --- a/tests/model/SQLQueryTest.php +++ b/tests/model/SQLQueryTest.php @@ -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()); }