From 47561690a425aa38f61ef9afbc6c417c5b89fbe1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 5 Jun 2024 17:19:24 +0900 Subject: [PATCH] fix: Model::find() returns incorrect data with casting --- system/Model.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/system/Model.php b/system/Model.php index 03ca7525ec76..8a3fad8c55eb 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,10 @@ protected function doFind(bool $singleton, $id = null) $builder->where($this->table . '.' . $this->deletedField, 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 +209,24 @@ 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 (isset($row)) { + return $this->convertToReturnType($row, $returnType); + } + + foreach ($rows as $i => $row) { + $rows[$i] = $this->convertToReturnType($row, $returnType); + } + + return $rows; } - return $row; + return $row ?? $rows; } /**