From 906d42603474ad16df6102d8c273ebb91b2d0656 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Tue, 16 Feb 2021 23:53:57 +1300 Subject: [PATCH] BUG Nomalise boolean ORM filter in to 1/0 --- src/ORM/FieldType/DBBoolean.php | 4 ++++ src/ORM/Filters/ExactMatchFilter.php | 4 ++-- tests/php/ORM/DataListTest.php | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/ORM/FieldType/DBBoolean.php b/src/ORM/FieldType/DBBoolean.php index 5e2ddc3a25b..1f70f87efeb 100644 --- a/src/ORM/FieldType/DBBoolean.php +++ b/src/ORM/FieldType/DBBoolean.php @@ -96,4 +96,8 @@ public function prepValueForDB($value) } return $value ? 1 : 0; } + + public function RAW() { + return $this->prepValueForDB($this->getValue()); + } } diff --git a/src/ORM/Filters/ExactMatchFilter.php b/src/ORM/Filters/ExactMatchFilter.php index 349eb8d142a..5a40fa882a5 100644 --- a/src/ORM/Filters/ExactMatchFilter.php +++ b/src/ORM/Filters/ExactMatchFilter.php @@ -53,7 +53,7 @@ protected function oneFilter(DataQuery $query, $inclusive) { $this->model = $query->applyRelation($this->relation); $field = $this->getDbName(); - $value = $this->getValue(); + $value = $this->getDbFormattedValue(); // Null comparison check if ($value === null) { @@ -77,7 +77,7 @@ protected function oneFilter(DataQuery $query, $inclusive) } $clause = [$where => $value]; - + return $this->aggregate ? $this->applyAggregate($query, $clause) : $query->where($clause); diff --git a/tests/php/ORM/DataListTest.php b/tests/php/ORM/DataListTest.php index 9658f52d1f9..59224c44dee 100755 --- a/tests/php/ORM/DataListTest.php +++ b/tests/php/ORM/DataListTest.php @@ -793,6 +793,20 @@ public function testMultipleFilterWithNoMatch() $this->assertEquals(0, $list->count()); } + public function testFilterByBoolean() { + $list = Player::get(); + $list = $list->filter(['IsRetired' => false]); + $this->assertEquals(3, $list->count(), 'Some records have IsRetired set to false'); + $allFalse = $list->column('IsRetired'); + $this->assertNotContains(true, $allFalse, 'Filter false should filter all true values'); + + $list = Player::get(); + $list = $list->filter(['IsRetired' => true]); + $this->assertEquals(1, $list->count(), 'One records has IsRetired set to true'); + $allTrue = $list->column('IsRetired'); + $this->assertNotContains(false, $allTrue, 'Filter true should filter all false values'); + } + /** * $list->filter(['Name'=>'bob, 'Age'=>21]); // bob with the age 21 */