mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API Allow using SearchFilters in DataList::exclude()
This commit is contained in:
parent
79b3f8ac45
commit
2faf7d112d
@ -500,28 +500,38 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
|
|||||||
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
|
||||||
*
|
*
|
||||||
|
@ -501,7 +501,7 @@ class DataListTest extends SapphireTest {
|
|||||||
*/
|
*/
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -514,10 +514,18 @@ class DataListTest extends SapphireTest {
|
|||||||
$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
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user