Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jun 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion model/DataQuery.php
Expand Up @@ -461,7 +461,7 @@ function where($filter) {
function whereAny($filter) { function whereAny($filter) {
if($filter) { if($filter) {
$clone = $this; $clone = $this;
$clone->query->whereAny($filter); $clone->query->addWhereAny($filter);
return $clone; return $clone;
} else { } else {
return $this; return $this;
Expand Down
16 changes: 15 additions & 1 deletion model/SQLQuery.php
Expand Up @@ -738,14 +738,28 @@ public function where($where) {
return $this->setWhere($where); 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. * @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(); if(is_string($filters)) $filters = func_get_args();
$clause = implode(" OR ", $filters); $clause = implode(" OR ", $filters);
return $this->setWhere($clause); 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. * Use the disjunctive operator 'OR' to join filter expressions in the WHERE clause.
Expand Down
11 changes: 11 additions & 0 deletions tests/model/DataListTest.php
Expand Up @@ -437,6 +437,17 @@ public function testMultipleExclude() {
$list->exclude(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob')); $list->exclude(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob'));
$this->assertEquals(2, $list->count()); $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 * $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43
Expand Down
4 changes: 2 additions & 2 deletions tests/model/SQLQueryTest.php
Expand Up @@ -258,11 +258,11 @@ public function testInnerJoin() {
); );
} }


public function testWhereAny() { public function testSetWhereAny() {
$query = new SQLQuery(); $query = new SQLQuery();
$query->setFrom('MyTable'); $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()); $this->assertEquals("SELECT * FROM MyTable WHERE (Monkey = 'Chimp' OR Color = 'Brown')",$query->sql());
} }


Expand Down