Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
API Allow using SearchFilters in DataList::exclude()
  • Loading branch information
simonwelsh committed Oct 9, 2012
1 parent 79b3f8a commit 2faf7d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
44 changes: 27 additions & 17 deletions model/DataList.php
Expand Up @@ -500,27 +500,37 @@ public function exclude() {
throw new InvalidArgumentException('Incorrect number of arguments passed to exclude()'); throw new InvalidArgumentException('Incorrect number of arguments passed to exclude()');
} }


$SQL_Statements = array(); return $this->alterDataQuery(function($query, $list) use ($whereArguments) {
foreach($whereArguments as $fieldName => $value) { $subquery = $query->disjunctiveGroup();
if($fieldName == 'ID') {
$fieldName = sprintf('"%s"."ID"', ClassInfo::baseDataClass($this->dataClass));
} else {
$fieldName = '"' . Convert::raw2sql($fieldName) . '"';
}


if(is_array($value)){ foreach($whereArguments as $field => $value) {
$SQL_Statements[] = ($fieldName . ' NOT IN (\''.implode('\',\'', Convert::raw2sql($value)).'\')'); $fieldArgs = explode(':', $field);
} else { $field = array_shift($fieldArgs);
$SQL_Statements[] = ($fieldName . ' != \''.Convert::raw2sql($value).'\''); $filterType = array_shift($fieldArgs);
$modifiers = $fieldArgs;
$list->excludeFilterContext($field, $filterType, $modifiers, $value, $subquery);
} }
}

if(!count($SQL_Statements)) return $this;

return $this->alterDataQuery_30(function($query) use ($SQL_Statements){
$query->whereAny($SQL_Statements);
}); });
} }

/**
* Translates the comparisator to the sql query
*
* @param string $field - the fieldname in the db
* @param string $comparisators - example StartsWith, relates to a filtercontext
* @param string $value - the value that the filtercontext will use for matching
* @param DataQuery $dataQuery - The (sub)query to add the exclusion clauses to
* @todo Deprecated SearchContexts and pull their functionality into the core of the ORM
*/
private function excludeFilterContext($field, $comparisators, $modifiers, $value, $dataQuery) {
$t = singleton($this->dataClass())->dbObject($field);
$className = "{$comparisators}Filter";
if(!class_exists($className)){
throw new InvalidArgumentException('There are no '.$comparisators.' comparisator');
}
$t = new $className($field, $value, $modifiers);
$t->exclude($dataQuery);
}


/** /**
* This method returns a copy of this list that does not contain any DataObjects that exists in $list * This method returns a copy of this list that does not contain any DataObjects that exists in $list
Expand Down
12 changes: 10 additions & 2 deletions tests/model/DataListTest.php
Expand Up @@ -501,7 +501,7 @@ public function testMultipleExcludeWithMiss() {
*/ */
public function testMultipleExclude() { public function testMultipleExclude() {
$list = DataObjectTest_TeamComment::get(); $list = DataObjectTest_TeamComment::get();
$list->exclude(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob')); $list = $list->exclude(array('Name'=>'Bob', 'Comment'=>'This is a team comment by Bob'));
$this->assertEquals(2, $list->count()); $this->assertEquals(2, $list->count());
} }


Expand All @@ -514,9 +514,17 @@ public function testExcludeOnFilter() {
$list = $list->exclude('Name', 'Bob'); $list = $list->exclude('Name', 'Bob');


$this->assertContains( $this->assertContains(
'WHERE ("Comment" = \'Phil is a unique guy, and comments on team2\') AND ("Name" != \'Bob\')', 'WHERE ("DataObjectTest_TeamComment"."Comment" = '
. '\'Phil is a unique guy, and comments on team2\') '
. 'AND (("DataObjectTest_TeamComment"."Name" != \'Bob\'))',
$list->sql()); $list->sql());
} }

public function testExcludeWithSearchFilter() {
$list = DataObjectTest_TeamComment::get();
$list = $list->exclude('Name:LessThan', 'Bob');
$this->assertContains('WHERE (("DataObjectTest_TeamComment"."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

0 comments on commit 2faf7d1

Please sign in to comment.