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 8c459cf commit 4756169
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 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,35 @@ protected function doFind(bool $singleton, $id = null)
$builder->where($this->table . '.' . $this->deletedField, 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 (isset($row)) {
return $this->convertToReturnType($row, $returnType);
}

foreach ($rows as $i => $row) {
$rows[$i] = $this->convertToReturnType($row, $returnType);
}

return $rows;
}

return $row;
return $row ?? $rows;
}

/**
Expand Down

0 comments on commit 4756169

Please sign in to comment.