diff --git a/system/Model.php b/system/Model.php index b3ecfc653943..03ca7525ec76 100644 --- a/system/Model.php +++ b/system/Model.php @@ -210,7 +210,7 @@ protected function doFind(bool $singleton, $id = null) $row = $builder->get()->getResult($this->tempReturnType); } - if ($useCast) { + if ($useCast && $row !== null) { $row = $this->convertToReturnType($row, $returnType); $this->tempReturnType = $returnType; @@ -318,7 +318,7 @@ protected function doFirst() $row = $builder->limit(1, 0)->get()->getFirstRow($this->tempReturnType); - if ($useCast) { + if ($useCast && $row !== null) { $row = $this->convertToReturnType($row, $returnType); $this->tempReturnType = $returnType; diff --git a/tests/system/Models/DataConverterModelTest.php b/tests/system/Models/DataConverterModelTest.php index cd50df0d8b00..4dd6da072f5e 100644 --- a/tests/system/Models/DataConverterModelTest.php +++ b/tests/system/Models/DataConverterModelTest.php @@ -43,6 +43,16 @@ public function testFindAsArray(): void $this->seeInDatabase('user', ['name' => 'Sm9obiBTbWl0aA==']); } + public function testFindAsArrayReturnsNull(): void + { + $this->createModel(UserCastsTimestampModel::class); + $this->db->table('user')->truncate(); + + $user = $this->model->find(1); + + $this->assertNull($user); + } + /** * @return int|string Insert ID */ @@ -102,6 +112,16 @@ public function testFindAllAsArray(): void $this->assertInstanceOf(Time::class, $users[1]['created_at']); } + public function testFindAllAsArrayReturnsNull(): void + { + $this->createModel(UserCastsTimestampModel::class); + $this->db->table('user')->truncate(); + + $users = $this->model->findAll(); + + $this->assertSame([], $users); + } + private function prepareTwoRecords(): void { $this->prepareOneRecord(); @@ -170,6 +190,16 @@ public function testFirstAsArray(): void $this->assertInstanceOf(Time::class, $user['created_at']); } + public function testFirstAsArrayReturnsNull(): void + { + $this->createModel(UserCastsTimestampModel::class); + $this->db->table('user')->truncate(); + + $user = $this->model->first(); + + $this->assertNull($user); + } + public function testFirstAsObject(): void { $this->prepareTwoRecords();