From af3a9f3ec8a5465f841c5aa8ee1faf40c1b76bf4 Mon Sep 17 00:00:00 2001 From: UndefinedOffset Date: Tue, 17 Apr 2018 13:37:02 -0300 Subject: [PATCH] FIX: Duplicating many_many relationships looses the extra fields (fixes #7973) --- model/DataObject.php | 16 +++++++++++++++- tests/model/DataObjectDuplicationTest.php | 11 ++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/model/DataObject.php b/model/DataObject.php index 84d6f5dac..43a9aa34c 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -585,7 +585,21 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity private function duplicateRelations($sourceObject, $destinationObject, $name) { $relations = $sourceObject->$name(); if ($relations) { - if ($relations instanceOf RelationList) { //many-to-something relation + if ($relations instanceOf ManyManyList) { //many-to-many relation + $source = $sourceObject->getManyManyComponents($name); + $extraFieldNames = $source->getExtraFields(); + + if ($relations->Count() > 0) { //with more than one thing it is related to + foreach($relations as $relation) { + // Merge extra fields + $extraFields = array(); + foreach ($extraFieldNames as $fieldName => $fieldType) { + $extraFields[$fieldName] = $relation->getField($fieldName); + } + $destinationObject->$name()->add($relation, $extraFields); + } + } + } else if ($relations instanceOf RelationList) { //many-to-something relation if ($relations->Count() > 0) { //with more than one thing it is related to foreach($relations as $relation) { $destinationObject->$name()->add($relation); diff --git a/tests/model/DataObjectDuplicationTest.php b/tests/model/DataObjectDuplicationTest.php index e3b24fece..2fcab1c4d 100644 --- a/tests/model/DataObjectDuplicationTest.php +++ b/tests/model/DataObjectDuplicationTest.php @@ -77,7 +77,7 @@ class DataObjectDuplicationTest extends SapphireTest { //create relations $one->twos()->add($two); - $one->threes()->add($three); + $one->threes()->add($three, array('TestExtra'=>'three')); $one = DataObject::get_by_id("DataObjectDuplicateTestClass1", $one->ID); $two = DataObject::get_by_id("DataObjectDuplicateTestClass2", $two->ID); @@ -115,6 +115,9 @@ class DataObjectDuplicationTest extends SapphireTest { "Match between relation of copy and the original"); $this->assertEquals($one->ID, $threeCopy->ones()->First()->ID, "Match between relation of copy and the original"); + + $this->assertEquals('three', $oneCopy->threes()->First()->TestExtra, + "Match between extra field of copy and the original"); } } @@ -133,6 +136,12 @@ class DataObjectDuplicateTestClass1 extends DataObject implements TestOnly { private static $many_many = array( 'threes' => 'DataObjectDuplicateTestClass3' ); + + private static $many_many_extraFields = array( + 'threes' => array( + 'TestExtra' => 'Varchar' + ) + ); } class DataObjectDuplicateTestClass2 extends DataObject implements TestOnly {