Skip to content

Commit

Permalink
ENH Respect new has_one config
Browse files Browse the repository at this point in the history
has_one can now optionally have an array configuration to allow
supporting multiple reciprocal has_many in a single has_one relation.
  • Loading branch information
GuySartorelli committed Dec 4, 2023
1 parent dba1bbd commit 3fbd94b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/DataDifferencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions src/RecursivePublishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 3fbd94b

Please sign in to comment.