Skip to content

Commit

Permalink
fix: Model::find() returns incorrect data with casting
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jun 5, 2024
1 parent fb816b1 commit 977059b
Showing 1 changed file with 27 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

0 comments on commit 977059b

Please sign in to comment.