diff --git a/generator/lib/builder/om/PHP5ObjectBuilder.php b/generator/lib/builder/om/PHP5ObjectBuilder.php index 1fba18ff8..538d66e76 100644 --- a/generator/lib/builder/om/PHP5ObjectBuilder.php +++ b/generator/lib/builder/om/PHP5ObjectBuilder.php @@ -3818,26 +3818,36 @@ protected function addRefFKSet(&$script, $refFK) * * @param PropelCollection \${$inputCollection} A Propel collection. * @param PropelPDO \$con Optional connection object - * @return ".$this->getObjectClassname()." The current object (for fluent API support) + * @return " . $this->getObjectClassname() . " The current object (for fluent API support) */ public function set{$relatedName}(PropelCollection \${$inputCollection}, PropelPDO \$con = null) { \${$inputCollection}ToDelete = \$this->get{$relatedName}(new Criteria(), \$con)->diff(\${$inputCollection}); +"; - \$this->{$inputCollection}ScheduledForDeletion = unserialize(serialize(\${$inputCollection}ToDelete)); + if ($refFK->isAtLeastOneLocalPrimaryKey()) { + $script .= " + //since at least one column in the foreign key is at the same time a PK + //we can not just set a PK to NULL in the lines below. We have to store + //a backup of all values, so we are able to manipulate these items based on the onDelete value later. + \$this->{$inputCollection}ScheduledForDeletion = clone \${$inputCollection}ToDelete; +"; + } else { + $script .= " + \$this->{$inputCollection}ScheduledForDeletion = \${$inputCollection}ToDelete; +"; + } + $script .= " foreach (\${$inputCollection}ToDelete as \${$inputCollectionEntry}Removed) { \${$inputCollectionEntry}Removed->set{$relCol}(null); } - \$this->{$collName} = null; foreach (\${$inputCollection} as \${$inputCollectionEntry}) { \$this->add{$relatedObjectClassName}(\${$inputCollectionEntry}); } - \$this->{$collName} = \${$inputCollection}; \$this->{$collName}Partial = false; - return \$this; } "; diff --git a/generator/lib/model/ForeignKey.php b/generator/lib/model/ForeignKey.php index b36be0d3c..34ff31f3a 100644 --- a/generator/lib/model/ForeignKey.php +++ b/generator/lib/model/ForeignKey.php @@ -580,6 +580,24 @@ public function isLocalPrimaryKey() !array_diff($localPKCols, $localCols)); } + /** + * Whether at least one local column is also a primary key. + * + * @return boolean True if there is at least one column that is a primary key + */ + public function isAtLeastOneLocalPrimaryKey() + { + $localCols = $this->getLocalColumnObjects(); + + foreach ($localCols as $localCol) { + if ($this->getTable()->getColumn($localCol->getName())->isPrimaryKey()) { + return true; + } + } + + return false; + } + /** * Set whether this foreign key should have its creation sql generated. * @param boolean $v Value to assign to skipSql.