Skip to content

Commit 186a426

Browse files
Jianbinzhuchrispenny
authored andcommitted
Add exclude_from_fixture_relationships in ManyManyThroughFields
1 parent e4d36c0 commit 186a426

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

src/Service/FixtureService.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,28 @@ private function addDataObjectManyManyFields(DataObject $dataObject): void
419419
continue;
420420
}
421421

422+
// Relationships are sometimes defined as ClassName.FieldName. Drop the .FieldName
423+
$cleanRelationshipClassName = strtok($relationClassName, '.');
424+
425+
// Check to see if this class has requested that it not be included in relationship maps
426+
$excludeClass = Config::inst()->get($cleanRelationshipClassName, 'exclude_from_fixture_relationships');
427+
428+
// Yup, exclude this class
429+
if ($excludeClass) {
430+
continue;
431+
}
432+
433+
// Check to see if this particular relationship wants to be excluded
434+
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
435+
$dataObject->ClassName,
436+
$relationFieldName
437+
);
438+
439+
// Yup, exclude this relationship
440+
if ($excludeRelationship) {
441+
continue;
442+
}
443+
422444
// TL;DR: many_many is really tough. Developers could choose to define it only in one direction, or in
423445
// both directions, and they could choose to define it either with, or without dot notation in either
424446
// direction
@@ -497,6 +519,17 @@ private function addDataObjectManyManyThroughFields(DataObject $dataObject): voi
497519
continue;
498520
}
499521

522+
// Check to see if this particular relationship wants to be excluded
523+
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
524+
$dataObject->ClassName,
525+
$relationFieldName
526+
);
527+
528+
// Yup, exclude this relationship
529+
if ($excludeRelationship) {
530+
continue;
531+
}
532+
500533
// This should always simply be defined as the class name (no dot notation)
501534
$throughClass = $relationshipValue['through'];
502535
$represented = false;
@@ -537,6 +570,14 @@ private function addDataObjectManyManyThroughFields(DataObject $dataObject): voi
537570
$relatedObjects = $dataObject->relField($relationFieldName);
538571

539572
foreach ($relatedObjects as $relatedObject) {
573+
// Check to see if this class has requested that it not be included in relationship maps
574+
$excludeClass = Config::inst()->get($relatedObject->ClassName, 'exclude_from_fixture_relationships');
575+
576+
// Yup, exclude this class
577+
if ($excludeClass) {
578+
continue;
579+
}
580+
540581
// Add the related DataObject as one of our resolved relationships
541582
$resolvedRelationships[] = sprintf('=>%s.%s', $relatedObject->ClassName, $relatedObject->ID);
542583

0 commit comments

Comments
 (0)