FIX: Duplicating many_many relationships looses the extra fields (fixes #7973)

This commit is contained in:
UndefinedOffset 2018-04-17 13:37:02 -03:00 committed by Daniel Hensby
parent f5c1f181bb
commit af3a9f3ec8
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
2 changed files with 25 additions and 2 deletions

View File

@ -585,7 +585,21 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
private function duplicateRelations($sourceObject, $destinationObject, $name) { private function duplicateRelations($sourceObject, $destinationObject, $name) {
$relations = $sourceObject->$name(); $relations = $sourceObject->$name();
if ($relations) { 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 if ($relations->Count() > 0) { //with more than one thing it is related to
foreach($relations as $relation) { foreach($relations as $relation) {
$destinationObject->$name()->add($relation); $destinationObject->$name()->add($relation);

View File

@ -77,7 +77,7 @@ class DataObjectDuplicationTest extends SapphireTest {
//create relations //create relations
$one->twos()->add($two); $one->twos()->add($two);
$one->threes()->add($three); $one->threes()->add($three, array('TestExtra'=>'three'));
$one = DataObject::get_by_id("DataObjectDuplicateTestClass1", $one->ID); $one = DataObject::get_by_id("DataObjectDuplicateTestClass1", $one->ID);
$two = DataObject::get_by_id("DataObjectDuplicateTestClass2", $two->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"); "Match between relation of copy and the original");
$this->assertEquals($one->ID, $threeCopy->ones()->First()->ID, $this->assertEquals($one->ID, $threeCopy->ones()->First()->ID,
"Match between relation of copy and the original"); "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( private static $many_many = array(
'threes' => 'DataObjectDuplicateTestClass3' 'threes' => 'DataObjectDuplicateTestClass3'
); );
private static $many_many_extraFields = array(
'threes' => array(
'TestExtra' => 'Varchar'
)
);
} }
class DataObjectDuplicateTestClass2 extends DataObject implements TestOnly { class DataObjectDuplicateTestClass2 extends DataObject implements TestOnly {