Permalink
Browse files

BUG Fixed DataList filtering and excluding by ID.

If a filter or exclude by ID was applied across more than one table it
would throw an ambiguous column error as the table name was not specified.
  • Loading branch information...
1 parent 682a6a0 commit 119da095491c4b9860058ec7e2162a6063386f22 @ajshort ajshort committed with sminnee Jun 23, 2012
Showing with 26 additions and 4 deletions.
  1. +15 −3 model/DataList.php
  2. +11 −1 tests/model/DataListTest.php
View
@@ -253,7 +253,13 @@ public function addFilter($filterArray) {
$comparisor = $this->applyFilterContext($field, $fieldArg, $value);
}
} else {
- $SQL_Statements[] = '"'.Convert::raw2sql($field).'" '.$customQuery;
+ if($field == 'ID') {
+ $field = sprintf('"%s"."ID"', ClassInfo::baseDataClass($this->dataClass));
+ } else {
+ $field = '"' . Convert::raw2sql($field) . '"';
+ }
+
+ $SQL_Statements[] = $field . ' ' . $customQuery;
}
}
if(count($SQL_Statements)) {
@@ -350,10 +356,16 @@ public function 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) . '"';
+ }
+
if(is_array($value)){
- $SQL_Statements[] = ('"'.$fieldName.'" NOT IN (\''.implode('\',\'', Convert::raw2sql($value)).'\')');
+ $SQL_Statements[] = ($fieldName . ' NOT IN (\''.implode('\',\'', Convert::raw2sql($value)).'\')');
} else {
- $SQL_Statements[] = ('"'.$fieldName.'" != \''.Convert::raw2sql($value).'\'');
+ $SQL_Statements[] = ($fieldName . ' != \''.Convert::raw2sql($value).'\'');
}
}
$this->dataQuery->whereAny($SQL_Statements);
View
12 tests/model/DataListTest.php 100755 → 100644
@@ -397,7 +397,17 @@ public function testFilterArrayInArray() {
$this->assertEquals(1, $list->count(), 'There should be one comments');
$this->assertEquals('Bob', $list->first()->Name, 'Only comment should be from Bob');
}
-
+
+ public function testFilterAndExcludeById() {
+ $id = $this->idFromFixture('DataObjectTest_SubTeam', 'subteam1');
+ $list = DataObjectTest_SubTeam::get()->filter('ID', $id);
+ $this->assertEquals($id, $list->first()->ID);
+
+ $list = DataObjectTest_SubTeam::get();
+ $this->assertEquals(3, count($list));
+ $this->assertEquals(2, count($list->exclude('ID', $id)));
+ }
+
/**
* $list->exclude('Name', 'bob'); // exclude bob from list
*/

0 comments on commit 119da09

Please sign in to comment.