Skip to content

Commit

Permalink
optimize Model::initEntityIdAndAssertUnchanged hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 24, 2023
1 parent ac9dcd6 commit d4ec429
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ private function initEntityIdAndAssertUnchanged(): void
if ($this->_entityId === null) {
// set entity ID to the first seen ID
$this->_entityId = $id;
} elseif (!$this->compare($this->idField, $this->_entityId)) {
} elseif ($this->_entityId !== $id && !$this->compare($this->idField, $this->_entityId)) {
$this->unload(); // data for different ID were loaded, make sure to discard them

throw (new Exception('Model instance is an entity, ID cannot be changed to a different one'))
Expand Down
2 changes: 1 addition & 1 deletion tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testEntityReloadWithDifferentIdException(): void
}, null, Model::class)();

$this->expectException(Exception::class);
$this->expectExceptionMessageMatches('~entity.+different~');
$this->expectExceptionMessageMessage('Model instance is an entity, ID cannot be changed to a different one');
$m->reload();
}

Expand Down

0 comments on commit d4ec429

Please sign in to comment.