Skip to content

Commit

Permalink
Merge pull request codeigniter4#8933 from kenjis/fix-Model-find-with-…
Browse files Browse the repository at this point in the history
…casts

fix: Model::find() returns incorrect data with casting
  • Loading branch information
kenjis authored Jun 8, 2024
2 parents be7b7f6 + 977059b commit 801b4f3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
43 changes: 27 additions & 16 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -198,25 +198,44 @@ 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) {
$row = $builder->where($this->table . '.' . $this->primaryKey, $id)
->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;
}

/**
Expand All @@ -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();
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Models/DataConverterModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 801b4f3

Please sign in to comment.