Skip to content

Commit

Permalink
ENH Respect new has_one config (#427)
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 authored Dec 12, 2023
1 parent dba1bbd commit 1e73253
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"require": {
"php": "^8.1",
"silverstripe/framework": "^5",
"silverstripe/framework": "^5.2",
"symfony/cache": "^6.1",
"silverstripe/vendor-plugin": "^2"
},
Expand All @@ -44,4 +44,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
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 1e73253

Please sign in to comment.