From fe4b90edc0ead9c6c77d606101bfbf568a963fb4 Mon Sep 17 00:00:00 2001 From: UndefinedOffset Date: Wed, 18 Apr 2018 10:45:51 -0300 Subject: [PATCH] FIX: Duplicating many_many relationships looses the extra fields in 4.0 --- src/ORM/DataObject.php | 14 +++++++++++++- tests/php/ORM/DataObjectDuplicationTest.php | 8 +++++++- tests/php/ORM/DataObjectDuplicationTest/Class1.php | 6 ++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index f8ad2560c..c6099fd7d 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -459,8 +459,20 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity // Copy all components from source to destination $source = $sourceObject->getManyManyComponents($manyManyName); $dest = $destinationObject->getManyManyComponents($manyManyName); + + if ($source instanceof UnsavedRelationList) { + $extraFieldNames = array(); + } else { + $extraFieldNames = $source->getExtraFields(); + } + foreach ($source as $item) { - $dest->add($item); + // Merge extra fields + $extraFields = array(); + foreach ($extraFieldNames as $fieldName => $fieldType) { + $extraFields[$fieldName] = $item->getField($fieldName); + } + $dest->add($item, $extraFields); } } diff --git a/tests/php/ORM/DataObjectDuplicationTest.php b/tests/php/ORM/DataObjectDuplicationTest.php index b3186d53f..09c88d821 100644 --- a/tests/php/ORM/DataObjectDuplicationTest.php +++ b/tests/php/ORM/DataObjectDuplicationTest.php @@ -102,7 +102,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(DataObjectDuplicationTest\Class1::class, $one->ID); $two = DataObject::get_by_id(DataObjectDuplicationTest\Class2::class, $two->ID); @@ -161,6 +161,12 @@ class DataObjectDuplicationTest extends SapphireTest $threeCopy->ones()->First()->ID, "Match between relation of copy and the original" ); + + $this->assertEquals( + 'three', + $oneCopy->threes()->byID($three->ID)->TestExtra, + "Match between extra field of copy and the original" + ); } public function testDuplicateManyManyFiltered() diff --git a/tests/php/ORM/DataObjectDuplicationTest/Class1.php b/tests/php/ORM/DataObjectDuplicationTest/Class1.php index 57a6c8302..b3a5fd3ef 100644 --- a/tests/php/ORM/DataObjectDuplicationTest/Class1.php +++ b/tests/php/ORM/DataObjectDuplicationTest/Class1.php @@ -20,4 +20,10 @@ class Class1 extends DataObject implements TestOnly private static $many_many = array( 'threes' => Class3::class ); + + private static $many_many_extraFields = array( + 'threes' => array( + 'TestExtra' => 'Varchar' + ) + ); }