Merge pull request #8009 from webbuilders-group/duplicate-many-many-fix

FIX: Duplicating many_many relationships looses the extra fields (fixes #7973)
This commit is contained in:
Daniel Hensby 2018-04-18 13:11:22 +01:00 committed by GitHub
commit f30cd61cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -585,7 +585,20 @@ 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
$extraFieldNames = $relations->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);

View File

@ -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 {