Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

API Allow using SearchFilters in DataList::exclude()

  • Loading branch information...
commit 2faf7d112d1d0a58724e393d08cecf0790f2e9b1 1 parent 79b3f8a
@simonwelsh simonwelsh authored
Showing with 37 additions and 19 deletions.
  1. +27 −17 model/DataList.php
  2. +10 −2 tests/model/DataListTest.php
View
44 model/DataList.php
@@ -500,27 +500,37 @@ public function exclude() {
throw new InvalidArgumentException('Incorrect number of arguments passed to exclude()');
}
- $SQL_Statements = array();
- foreach($whereArguments as $fieldName => $value) {
- if($fieldName == 'ID') {
- $fieldName = sprintf('"%s"."ID"', ClassInfo::baseDataClass($this->dataClass));
- } else {
- $fieldName = '"' . Convert::raw2sql($fieldName) . '"';
- }
+ return $this->alterDataQuery(function($query, $list) use ($whereArguments) {
+ $subquery = $query->disjunctiveGroup();
- if(is_array($value)){
- $SQL_Statements[] = ($fieldName . ' NOT IN (\''.implode('\',\'', Convert::raw2sql($value)).'\')');
- } else {
- $SQL_Statements[] = ($fieldName . ' != \''.Convert::raw2sql($value).'\'');
+ foreach($whereArguments as $field => $value) {
+ $fieldArgs = explode(':', $field);
+ $field = array_shift($fieldArgs);
+ $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
View
12 tests/model/DataListTest.php
@@ -501,7 +501,7 @@ public function testMultipleExcludeWithMiss() {
*/
public function testMultipleExclude() {
$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());
}
@@ -514,9 +514,17 @@ public function testExcludeOnFilter() {
$list = $list->exclude('Name', 'Bob');
$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());
}
+
+ 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
Please sign in to comment.
Something went wrong with that request. Please try again.