mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX: Duplicating many_many relationships looses the extra fields (fixes #7973)
This commit is contained in:
parent
f5c1f181bb
commit
af3a9f3ec8
@ -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);
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user