diff --git a/search/filters/ExactMatchFilter.php b/search/filters/ExactMatchFilter.php index 515fa254e..dbcd2ed63 100644 --- a/search/filters/ExactMatchFilter.php +++ b/search/filters/ExactMatchFilter.php @@ -56,6 +56,10 @@ class ExactMatchFilter extends SearchFilter { // For queries using the default collation (no explicit case) we can use the WHERE .. IN .. syntax, // providing simpler SQL than many WHERE .. OR .. fragments. $column = $this->getDbName(); + // If values is an empty array, fall back to 3.1 behaviour and use empty string comparison + if(empty($values)) { + $values = array(''); + } $placeholders = DB::placeholders($values); return $query->where(array( "$column IN ($placeholders)" => $values @@ -109,6 +113,10 @@ class ExactMatchFilter extends SearchFilter { // For queries using the default collation (no explicit case) we can use the WHERE .. NOT IN .. syntax, // providing simpler SQL than many WHERE .. AND .. fragments. $column = $this->getDbName(); + // If values is an empty array, fall back to 3.1 behaviour and use empty string comparison + if(empty($values)) { + $values = array(''); + } $placeholders = DB::placeholders($values); return $query->where(array( "$column NOT IN ($placeholders)" => $values diff --git a/tests/model/DataListTest.php b/tests/model/DataListTest.php index 7d7b453bc..5c6ca0ac0 100755 --- a/tests/model/DataListTest.php +++ b/tests/model/DataListTest.php @@ -897,6 +897,26 @@ class DataListTest extends SapphireTest { $this->assertEquals(array('Bob'), $parameters); } + /** + * Test exact match filter with empty array items + */ + public function testEmptyFilter() { + $list = DataObjectTest_TeamComment::get(); + $list = $list->exclude('Name', array()); + + $sql = $list->sql($parameters); + $this->assertSQLContains('WHERE (("DataObjectTest_TeamComment"."Name" NOT IN (?)))', $sql); + $this->assertEquals(array(''), $parameters); + + + $list = DataObjectTest_TeamComment::get(); + $list = $list->filter('Name', array()); + + $sql = $list->sql($parameters); + $this->assertSQLContains('WHERE ("DataObjectTest_TeamComment"."Name" IN (?))', $sql); + $this->assertEquals(array(''), $parameters); + } + /** * $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43 */