Skip to content

Commit

Permalink
Revert some changes and allow the primary key to be an empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Jan 22, 2019
1 parent 5011f8e commit 3aaaee2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
26 changes: 13 additions & 13 deletions core-bundle/src/Resources/contao/library/Contao/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,19 +416,6 @@ 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 @@ -456,6 +443,19 @@ public function save()
return $this;
}

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

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

if ($intPk === null)
{
throw new \RuntimeException('The primary key has not been set');
}

// Update the row
$objDatabase->prepare("UPDATE " . static::$strTable . " %s WHERE " . \Database::quoteIdentifier(static::$strPk) . "=?")
->set($arrSet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,9 @@ public function register(Model $objModel)
$strPk = $objModel->getPk();
$varPk = $objModel->$strPk;

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

// Another model object is pointing to the DB record already
Expand Down

0 comments on commit 3aaaee2

Please sign in to comment.