Skip to content

Commit

Permalink
Merge pull request #5439 from kenjis/fix-Model-shouldUpdate
Browse files Browse the repository at this point in the history
fix: Model::save() may call unneeded countAllResults()
  • Loading branch information
kenjis committed Dec 7, 2021
2 parents 66e592c + faa474a commit 1e036bc
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,17 @@ public function set($key, $value = '', ?bool $escape = null)
*/
protected function shouldUpdate($data): bool
{
// When useAutoIncrement feature is disabled check
if (parent::shouldUpdate($data) === false) {
return false;
}

if ($this->useAutoIncrement === true) {
return true;
}

// When useAutoIncrement feature is disabled, check
// in the database if given record already exists
return parent::shouldUpdate($data)
&& $this->useAutoIncrement
? true
: $this->where($this->primaryKey, $this->getIdValue($data))->countAllResults() === 1;
return $this->where($this->primaryKey, $this->getIdValue($data))->countAllResults() === 1;
}

/**
Expand Down

0 comments on commit 1e036bc

Please sign in to comment.