mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Fix YamlFixture issues when specifying IDs
BUGFIX: Fix DataObject::write() with a specified ID and forceInsert to be true git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64798 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
3966f9a794
commit
bff5942338
@ -685,7 +685,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
|
|||||||
foreach($this->record as $k => $v) {
|
foreach($this->record as $k => $v) {
|
||||||
$this->changed[$k] = 2;
|
$this->changed[$k] = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
$firstWrite = true;
|
$firstWrite = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -697,7 +697,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Look for some changes to make
|
// Look for some changes to make
|
||||||
unset($this->changed['ID']);
|
if(!$forceInsert) unset($this->changed['ID']);
|
||||||
|
|
||||||
$hasChanges = false;
|
$hasChanges = false;
|
||||||
foreach($this->changed as $fieldName => $changed) {
|
foreach($this->changed as $fieldName => $changed) {
|
||||||
@ -706,7 +706,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($hasChanges || $forceWrite || !$this->record['ID']) {
|
if($hasChanges || $forceWrite || !$this->record['ID']) {
|
||||||
|
|
||||||
// New records have their insert into the base data table done first, so that they can pass the
|
// New records have their insert into the base data table done first, so that they can pass the
|
||||||
|
@ -134,13 +134,13 @@ class YamlFixture extends Object {
|
|||||||
foreach($fixtureContent as $dataClass => $items) {
|
foreach($fixtureContent as $dataClass => $items) {
|
||||||
foreach($items as $identifier => $fields) {
|
foreach($items as $identifier => $fields) {
|
||||||
$obj = new $dataClass();
|
$obj = new $dataClass();
|
||||||
|
|
||||||
// Populate the dictionary with the ID
|
// Populate the dictionary with the ID
|
||||||
foreach($fields as $fieldName => $fieldVal) {
|
foreach($fields as $fieldName => $fieldVal) {
|
||||||
if($obj->many_many($fieldName) || $obj->has_many($fieldName) || $obj->has_one($fieldName)) continue;
|
if($obj->many_many($fieldName) || $obj->has_many($fieldName) || $obj->has_one($fieldName)) continue;
|
||||||
$obj->$fieldName = $this->parseFixtureVal($fieldVal);
|
$obj->$fieldName = $this->parseFixtureVal($fieldVal);
|
||||||
}
|
}
|
||||||
$obj->write();
|
|
||||||
|
$obj->write(false, true);
|
||||||
|
|
||||||
// has to happen before relations in case a class is referring to itself
|
// has to happen before relations in case a class is referring to itself
|
||||||
$this->fixtureDictionary[$dataClass][$identifier] = $obj->ID;
|
$this->fixtureDictionary[$dataClass][$identifier] = $obj->ID;
|
||||||
|
@ -503,6 +503,28 @@ class DataObjectTest extends SapphireTest {
|
|||||||
$this->assertTrue(false, "Validated object threw an unexpected exception of type " . get_class($exception) . " from DataObject::write: " . $exception->getMessage());
|
$this->assertTrue(false, "Validated object threw an unexpected exception of type " . get_class($exception) . " from DataObject::write: " . $exception->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSubclassCreation() {
|
||||||
|
/* Creating a new object of a subclass should set the ClassName field correctly */
|
||||||
|
$obj = new DataObjectTest_SubTeam();
|
||||||
|
$obj->write();
|
||||||
|
$this->assertEquals("DataObjectTest_SubTeam", DB::query("SELECT ClassName FROM DataObjectTest_Team WHERE ID = $obj->ID")->value());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testForceInsert() {
|
||||||
|
/* If you set an ID on an object and pass forceInsert = true, then the object should be correctly created */
|
||||||
|
$obj = new DataObjectTest_SubTeam();
|
||||||
|
$obj->ID = 1001;
|
||||||
|
$obj->Title = 'asdfasdf';
|
||||||
|
$obj->SubclassDatabaseField = 'asdfasdf';
|
||||||
|
$obj->write(false, true);
|
||||||
|
|
||||||
|
$this->assertEquals("DataObjectTest_SubTeam", DB::query("SELECT ClassName FROM DataObjectTest_Team WHERE ID = $obj->ID")->value());
|
||||||
|
|
||||||
|
/* Check that it actually saves to the database with the correct ID */
|
||||||
|
$this->assertEquals("1001", DB::query("SELECT ID FROM DataObjectTest_SubTeam WHERE SubclassDatabaseField = 'asdfasdf'")->value());
|
||||||
|
$this->assertEquals("1001", DB::query("SELECT ID FROM DataObjectTest_Team WHERE Title = 'asdfasdf'")->value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DataObjectTest_Player extends Member implements TestOnly {
|
class DataObjectTest_Player extends Member implements TestOnly {
|
||||||
|
Loading…
Reference in New Issue
Block a user