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:
Normann Lou 2008-10-28 01:23:41 +00:00
parent 3966f9a794
commit bff5942338
3 changed files with 27 additions and 5 deletions

View File

@ -685,7 +685,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
foreach($this->record as $k => $v) {
$this->changed[$k] = 2;
}
$firstWrite = true;
}
@ -697,7 +697,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
}
// Look for some changes to make
unset($this->changed['ID']);
if(!$forceInsert) unset($this->changed['ID']);
$hasChanges = false;
foreach($this->changed as $fieldName => $changed) {
@ -706,7 +706,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
break;
}
}
if($hasChanges || $forceWrite || !$this->record['ID']) {
// New records have their insert into the base data table done first, so that they can pass the

View File

@ -134,13 +134,13 @@ class YamlFixture extends Object {
foreach($fixtureContent as $dataClass => $items) {
foreach($items as $identifier => $fields) {
$obj = new $dataClass();
// Populate the dictionary with the ID
foreach($fields as $fieldName => $fieldVal) {
if($obj->many_many($fieldName) || $obj->has_many($fieldName) || $obj->has_one($fieldName)) continue;
$obj->$fieldName = $this->parseFixtureVal($fieldVal);
}
$obj->write();
$obj->write(false, true);
// has to happen before relations in case a class is referring to itself
$this->fixtureDictionary[$dataClass][$identifier] = $obj->ID;

View File

@ -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());
}
}
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 {