Skip to content

Commit

Permalink
Do not allow registering a Model with an empty primary key
Browse files Browse the repository at this point in the history
The Registry should either categorically allow registering a model
without a primary key or categorically not allow it. This fix implements
the latter. It does not use `empty()` because that would overshoot and
prevent the primary key from being `0` or `"0"`, which may not be
recommendable, but does not have to be forbidden in this context.
  • Loading branch information
gmpf committed Dec 13, 2018
1 parent 41ca519 commit 3f33fc2
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ public function register(Model $objModel)
$strPk = $objModel->getPk();
$varPk = $objModel->$strPk;

if ($varPk === null || $varPk === '')
{
throw new \RuntimeException(
"Cannot register a model with a primary key that is null or empty string ($strTable::$strPk)"
);
}

// Another model object is pointing to the DB record already
if (isset($this->arrRegistry[$strTable][$varPk]))
{
Expand Down

0 comments on commit 3f33fc2

Please sign in to comment.