Skip to content

Commit

Permalink
prefer limit 2 and getRows()
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 17, 2022
1 parent 25e7e22 commit 7cc0378
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,20 @@ public function tryLoad(Model $model, $id): ?array
if ($id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY) {
$action = $this->action($model, 'select');

$selectRow = $action->getRow();
if ($selectRow === null) {
$action->limit($id === self::ID_LOAD_ANY ? 1 : 2);

$rowsRaw = $action->getRows();
if (count($rowsRaw) === 0) {
return null;
} elseif ($id === self::ID_LOAD_ONE && $action->getRow() !== null) {
} elseif (count($rowsRaw) !== 1) {
throw (new Exception('Ambiguous conditions, more than one record can be loaded.'))
->addMoreInfo('model', $model)
->addMoreInfo('id', null);
}

$id = $selectRow[$model->id_field];
$idRaw = reset($rowsRaw)[$model->id_field];

$row = $this->tryLoad($model, $id);
$row = $this->tryLoad($model, $idRaw);

return $row;
}
Expand Down
1 change: 1 addition & 0 deletions src/Persistence/Array_/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public function getRows(): array
*/
public function getRow(): ?array
{
$this->generator->rewind(); // TODO alternatively allow to fetch only once
$row = $this->generator->current();
$this->generator->next();

Expand Down

0 comments on commit 7cc0378

Please sign in to comment.