Skip to content

Commit

Permalink
Do not allow saving a Model with an invalid primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpf committed Dec 19, 2018
1 parent 3f33fc2 commit 5011f8e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions core-bundle/src/Resources/contao/library/Contao/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,19 @@ public function save()
throw new \RuntimeException('The model instance has been detached and cannot be saved');
}

$intPk = $this->{static::$strPk};

// Track primary key changes
if (isset($this->arrModified[static::$strPk]))
{
$intPk = $this->arrModified[static::$strPk];
}

if ($intPk !== null && empty($intPk))
{
throw new \RuntimeException('Model cannot be saved: Invalid primary key (' . static::$strPk . ')');
}

$objDatabase = \Database::getInstance();
$arrFields = $objDatabase->getFieldNames(static::$strTable);

Expand Down Expand Up @@ -443,14 +456,6 @@ public function save()
return $this;
}

$intPk = $this->{static::$strPk};

// Track primary key changes
if (isset($this->arrModified[static::$strPk]))
{
$intPk = $this->arrModified[static::$strPk];
}

// Update the row
$objDatabase->prepare("UPDATE " . static::$strTable . " %s WHERE " . \Database::quoteIdentifier(static::$strPk) . "=?")
->set($arrSet)
Expand Down

0 comments on commit 5011f8e

Please sign in to comment.