Skip to content

Commit 977059b

Browse files
committed
fix: Model::find() returns incorrect data with casting
1 parent fb816b1 commit 977059b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

system/Model.php

Lines changed: 27 additions & 16 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,44 @@ protected function doFind(bool $singleton, $id = null)
198198
$builder->where($this->table . '.' . $this->deletedField, null);
199199
}
200200

201+
$row = null;
202+
$rows = [];
203+
201204
if (is_array($id)) {
202-
$row = $builder->whereIn($this->table . '.' . $this->primaryKey, $id)
205+
$rows = $builder->whereIn($this->table . '.' . $this->primaryKey, $id)
203206
->get()
204207
->getResult($this->tempReturnType);
205208
} elseif ($singleton) {
206209
$row = $builder->where($this->table . '.' . $this->primaryKey, $id)
207210
->get()
208211
->getFirstRow($this->tempReturnType);
209212
} else {
210-
$row = $builder->get()->getResult($this->tempReturnType);
213+
$rows = $builder->get()->getResult($this->tempReturnType);
211214
}
212215

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

219-
return $row;
234+
if ($singleton) {
235+
return $row;
236+
}
237+
238+
return $rows;
220239
}
221240

222241
/**
@@ -230,15 +249,7 @@ protected function doFind(bool $singleton, $id = null)
230249
*/
231250
protected function doFindColumn(string $columnName)
232251
{
233-
$results = $this->select($columnName)->asArray()->find();
234-
235-
if ($this->useCasts()) {
236-
foreach ($results as $i => $row) {
237-
$results[$i] = $this->converter->fromDataSource($row);
238-
}
239-
}
240-
241-
return $results;
252+
return $this->select($columnName)->asArray()->find();
242253
}
243254

244255
/**

0 commit comments

Comments
 (0)