diff --git a/src/DataDifferencer.php b/src/DataDifferencer.php index 7436f781..e7f0f7b8 100644 --- a/src/DataDifferencer.php +++ b/src/DataDifferencer.php @@ -139,6 +139,10 @@ public function diffedData() continue; } + if (is_array($relSpec)) { + $relSpec = DataObject::getSchema()->hasOneComponent($this->fromRecord->ClassName, $relName); + } + // Create the actual column name $relField = "{$relName}ID"; // Using relation name instead of database column name, because of FileField etc. diff --git a/src/RecursivePublishable.php b/src/RecursivePublishable.php index c25698bc..0407b353 100644 --- a/src/RecursivePublishable.php +++ b/src/RecursivePublishable.php @@ -381,7 +381,9 @@ public function unlinkDisownedRelationship($source, $targetStage, $relationship) } // Find table and field to join on - $joinField = $schema->getRemoteJoinField(get_class($owner), $relationship, 'has_many', $polymorphic); + $hasManyDetails = $schema->getHasManyComponentDetails(get_class($owner), $relationship); + $polymorphic = $hasManyDetails['polymorphic']; + $joinField = $hasManyDetails['joinField']; $joinTable = DataObject::getSchema()->tableForField( $joinClass, $polymorphic ? "{$joinField}ID" : $joinField @@ -391,13 +393,20 @@ public function unlinkDisownedRelationship($source, $targetStage, $relationship) $targetTable = $versioned->stageTable($joinTable, $targetStage); $disowned = new SQLUpdate("\"{$targetTable}\""); if ($polymorphic) { + $where = [ + "\"{$targetTable}\".\"{$joinField}ID\"" => $owner->ID, + "\"{$targetTable}\".\"{$joinField}Class\"" => get_class($owner), + ]; + + if ($hasManyDetails['needsRelation']) { + $disowned->assign("\"{$joinField}Relation\"", null); + $where["\"{$targetTable}\".\"{$joinField}Relation\""] = $relationship; + } + $disowned ->assign("\"{$joinField}ID\"", 0) ->assign("\"{$joinField}Class\"", null) - ->addWhere([ - "\"{$targetTable}\".\"{$joinField}ID\"" => $owner->ID, - "\"{$targetTable}\".\"{$joinField}Class\"" => get_class($owner), - ]); + ->addWhere($where); } else { $disowned ->assign("\"{$joinField}\"", 0)