Skip to content

Commit 735ab5e

Browse files
committed
Add exclude_from_fixture_relationships in ManyManyThroughFields
1 parent dddece3 commit 735ab5e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/Service/FixtureService.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,28 @@ private function addDataObjectManyManyFields(DataObject $dataObject): void
392392
continue;
393393
}
394394

395+
// Relationships are sometimes defined as ClassName.FieldName. Drop the .FieldName
396+
$cleanRelationshipClassName = strtok($relationClassName, '.');
397+
398+
// Check to see if this class has requested that it not be included in relationship maps
399+
$excludeClass = Config::inst()->get($cleanRelationshipClassName, 'exclude_from_fixture_relationships');
400+
401+
// Yup, exclude this class
402+
if ($excludeClass) {
403+
continue;
404+
}
405+
406+
// Check to see if this particular relationship wants to be excluded
407+
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
408+
$dataObject->ClassName,
409+
$relationFieldName
410+
);
411+
412+
// Yup, exclude this relationship
413+
if ($excludeRelationship) {
414+
continue;
415+
}
416+
395417
// TL;DR: many_many is really tough. Developers could choose to define it only in one direction, or in
396418
// both directions, and they could choose to define it either with, or without dot notation in either
397419
// direction
@@ -470,6 +492,17 @@ private function addDataObjectManyManyThroughFields(DataObject $dataObject): voi
470492
continue;
471493
}
472494

495+
// Check to see if this particular relationship wants to be excluded
496+
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
497+
$dataObject->ClassName,
498+
$relationFieldName
499+
);
500+
501+
// Yup, exclude this relationship
502+
if ($excludeRelationship) {
503+
continue;
504+
}
505+
473506
// This should always simply be defined as the class name (no dot notation)
474507
$throughClass = $relationshipValue['through'];
475508
$represented = false;
@@ -510,6 +543,14 @@ private function addDataObjectManyManyThroughFields(DataObject $dataObject): voi
510543
$relatedObjects = $dataObject->relField($relationFieldName);
511544

512545
foreach ($relatedObjects as $relatedObject) {
546+
// Check to see if this class has requested that it not be included in relationship maps
547+
$excludeClass = Config::inst()->get($relatedObject->ClassName, 'exclude_from_fixture_relationships');
548+
549+
// Yup, exclude this class
550+
if ($excludeClass) {
551+
continue;
552+
}
553+
513554
// Add the related DataObject as one of our resolved relationships
514555
$resolvedRelationships[] = sprintf('=>%s.%s', $relatedObject->ClassName, $relatedObject->ID);
515556

0 commit comments

Comments
 (0)