Skip to content

Commit 801b4f3

Browse files
authored
Merge pull request #8933 from kenjis/fix-Model-find-with-casts
fix: Model::find() returns incorrect data with casting
2 parents be7b7f6 + 977059b commit 801b4f3

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-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
/**

tests/system/Models/DataConverterModelTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ public function testFindAsEntity(): void
100100
$this->assertInstanceOf(Time::class, $user->created_at);
101101
}
102102

103+
public function testFindArrayAsEntity(): void
104+
{
105+
$id = $this->prepareOneRecord();
106+
107+
$users = $this->model->asObject(User::class)->find([$id, 999]);
108+
109+
$this->assertCount(1, $users);
110+
$this->assertIsInt($users[0]->id);
111+
$this->assertInstanceOf(Time::class, $users[0]->created_at);
112+
}
113+
114+
public function testFindNullAsEntity(): void
115+
{
116+
$this->prepareOneRecord();
117+
118+
$users = $this->model->asObject(User::class)->find();
119+
120+
$this->assertCount(1, $users);
121+
$this->assertIsInt($users[0]->id);
122+
$this->assertInstanceOf(Time::class, $users[0]->created_at);
123+
}
124+
103125
public function testFindAllAsArray(): void
104126
{
105127
$this->prepareTwoRecords();

0 commit comments

Comments
 (0)