From fb816b15352fb2d8bd3be82948523d6b65f785d0 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 5 Jun 2024 17:34:43 +0900 Subject: [PATCH 1/2] test: add tests for Model::find() with casts --- .../system/Models/DataConverterModelTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/system/Models/DataConverterModelTest.php b/tests/system/Models/DataConverterModelTest.php index 4dd6da072f5e..f243871526f3 100644 --- a/tests/system/Models/DataConverterModelTest.php +++ b/tests/system/Models/DataConverterModelTest.php @@ -100,6 +100,28 @@ public function testFindAsEntity(): void $this->assertInstanceOf(Time::class, $user->created_at); } + public function testFindArrayAsEntity(): void + { + $id = $this->prepareOneRecord(); + + $users = $this->model->asObject(User::class)->find([$id, 999]); + + $this->assertCount(1, $users); + $this->assertIsInt($users[0]->id); + $this->assertInstanceOf(Time::class, $users[0]->created_at); + } + + public function testFindNullAsEntity(): void + { + $this->prepareOneRecord(); + + $users = $this->model->asObject(User::class)->find(); + + $this->assertCount(1, $users); + $this->assertIsInt($users[0]->id); + $this->assertInstanceOf(Time::class, $users[0]->created_at); + } + public function testFindAllAsArray(): void { $this->prepareTwoRecords(); From 977059bbc8f3614ee77e7f4c20e742a1c6e7a6cf Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 5 Jun 2024 17:19:24 +0900 Subject: [PATCH 2/2] fix: Model::find() returns incorrect data with casting --- system/Model.php | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/system/Model.php b/system/Model.php index 03ca7525ec76..d5df3f861b97 100644 --- a/system/Model.php +++ b/system/Model.php @@ -174,7 +174,7 @@ public function setTable(string $table) } /** - * Fetches the row of database from $this->table with a primary key + * Fetches the row(s) of database from $this->table with a primary key * matching $id. * This method works only with dbCalls. * @@ -198,8 +198,11 @@ protected function doFind(bool $singleton, $id = null) $builder->where($this->table . '.' . $this->deletedField, null); } + $row = null; + $rows = []; + if (is_array($id)) { - $row = $builder->whereIn($this->table . '.' . $this->primaryKey, $id) + $rows = $builder->whereIn($this->table . '.' . $this->primaryKey, $id) ->get() ->getResult($this->tempReturnType); } elseif ($singleton) { @@ -207,16 +210,32 @@ protected function doFind(bool $singleton, $id = null) ->get() ->getFirstRow($this->tempReturnType); } else { - $row = $builder->get()->getResult($this->tempReturnType); + $rows = $builder->get()->getResult($this->tempReturnType); } - if ($useCast && $row !== null) { - $row = $this->convertToReturnType($row, $returnType); - + if ($useCast) { $this->tempReturnType = $returnType; + + if ($singleton) { + if ($row === null) { + return null; + } + + return $this->convertToReturnType($row, $returnType); + } + + foreach ($rows as $i => $row) { + $rows[$i] = $this->convertToReturnType($row, $returnType); + } + + return $rows; } - return $row; + if ($singleton) { + return $row; + } + + return $rows; } /** @@ -230,15 +249,7 @@ protected function doFind(bool $singleton, $id = null) */ protected function doFindColumn(string $columnName) { - $results = $this->select($columnName)->asArray()->find(); - - if ($this->useCasts()) { - foreach ($results as $i => $row) { - $results[$i] = $this->converter->fromDataSource($row); - } - } - - return $results; + return $this->select($columnName)->asArray()->find(); } /**