Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

BUG Correct relation saving in DataObject->duplicateRelations()

This caused problems when duplicate() was used in the CMS UI
to duplicate a SiteTree object. Since every object of this type
has a ParentID relation, it copied this empty relation into
new "ghost page".

See silverstripe/silverstripe-cms#689
  • Loading branch information...
commit f61ab7305dca2de093078969864441e69fee18ff 1 parent d5510f5
@chillu chillu authored
Showing with 26 additions and 1 deletion.
  1. +1 −1  model/DataObject.php
  2. +25 −0 tests/model/DataObjectDuplicationTest.php
View
2  model/DataObject.php
@@ -499,7 +499,7 @@ private function duplicateRelations($sourceObject, $destinationObject, $name) {
}
}
} else { //one-to-one relation
- $destinationObject->$name = $relations;
+ $destinationObject->{"{$name}ID"} = $relations->ID;
}
}
}
View
25 tests/model/DataObjectDuplicationTest.php
@@ -1,6 +1,8 @@
<?php
class DataObjectDuplicationTest extends SapphireTest {
+
+ protected $usesDatabase = true;
protected $extraDataObjects = array(
'DataObjectDuplicateTestClass1',
@@ -28,6 +30,29 @@ public function testDuplicate() {
);
}
+ public function testDuplicateHasOne() {
+ $relationObj = new DataObjectDuplicateTestClass1();
+ $relationObj->text = 'class1';
+ $relationObj->write();
+
+ $orig = new DataObjectDuplicateTestClass2();
+ $orig->text = 'class2';
+ $orig->oneID = $relationObj->ID;
+ $orig->write();
+
+ $duplicate = $orig->duplicate();
+ $this->assertEquals($relationObj->ID, $duplicate->oneID,
+ 'Copies has_one relationship'
+ );
+ $this->assertEquals(2, DataObjectDuplicateTestClass2::get()->Count(),
+ 'Only creates a single duplicate'
+ );
+ $this->assertEquals(1, DataObjectDuplicateTestClass1::get()->Count(),
+ 'Does not create duplicate of has_one relationship'
+ );
+ }
+
+
public function testDuplicateManyManyClasses() {
//create new test classes below
$one = new DataObjectDuplicateTestClass1();
Please sign in to comment.
Something went wrong with that request. Please try again.