Skip to content

Commit eae7229

Browse files
committed
fix: Model::find() returns incorrect data with casting
1 parent 0bbf6b9 commit eae7229

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

system/Model.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function setTable(string $table)
174174
}
175175

176176
/**
177-
* Fetches the row of database from $this->table with a primary key
177+
* Fetches the row(s) of database from $this->table with a primary key
178178
* matching $id.
179179
* This method works only with dbCalls.
180180
*
@@ -198,25 +198,43 @@ protected function doFind(bool $singleton, $id = null)
198198
$builder->where($this->table . '.' . $this->deletedField, null);
199199
}
200200

201+
$rows = [];
202+
201203
if (is_array($id)) {
202-
$row = $builder->whereIn($this->table . '.' . $this->primaryKey, $id)
204+
$rows = $builder->whereIn($this->table . '.' . $this->primaryKey, $id)
203205
->get()
204206
->getResult($this->tempReturnType);
205207
} elseif ($singleton) {
206208
$row = $builder->where($this->table . '.' . $this->primaryKey, $id)
207209
->get()
208210
->getFirstRow($this->tempReturnType);
209211
} else {
210-
$row = $builder->get()->getResult($this->tempReturnType);
212+
$rows = $builder->get()->getResult($this->tempReturnType);
211213
}
212214

213-
if ($useCast && $row !== null) {
214-
$row = $this->convertToReturnType($row, $returnType);
215-
215+
if ($useCast) {
216216
$this->tempReturnType = $returnType;
217+
218+
if ($singleton) {
219+
if ($row === null) {
220+
return null;
221+
}
222+
223+
return $this->convertToReturnType($row, $returnType);
224+
}
225+
226+
foreach ($rows as $i => $row) {
227+
$rows[$i] = $this->convertToReturnType($row, $returnType);
228+
}
229+
230+
return $rows;
217231
}
218232

219-
return $row;
233+
if ($singleton) {
234+
return $row;
235+
}
236+
237+
return $rows;
220238
}
221239

222240
/**

0 commit comments

Comments
 (0)