BUGFIX YamlFixture->saveIntoDatabase(): In order to support reflexive relations which need a valid object ID, the record is written twice: first after populating all non-relational fields, then again after populating all relations (has_one, has_many, many_many). Fixes a bug where FileTest was testing onBeforeWrite() behaviour

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64005 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-10-09 15:14:03 +00:00
parent 320c30ba5d
commit eb5cd150d8

View File

@ -121,7 +121,11 @@ class YamlFixture extends Object {
/** /**
* Load a YAML fixture file into the database. * Load a YAML fixture file into the database.
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
*
* Caution: In order to support reflexive relations which need a valid object ID,
* the record is written twice: first after populating all non-relational fields,
* then again after populating all relations (has_one, has_many, many_many).
*/ */
public function saveIntoDatabase() { public function saveIntoDatabase() {
$parser = new Spyc(); $parser = new Spyc();
@ -130,12 +134,18 @@ 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();
$obj->write();
// Populate the dictionary with the ID // 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();
// 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;
// Populate all relations
foreach($fields as $fieldName => $fieldVal) { foreach($fields as $fieldName => $fieldVal) {
if($obj->many_many($fieldName) || $obj->has_many($fieldName)) { if($obj->many_many($fieldName) || $obj->has_many($fieldName)) {
$parsedItems = array(); $parsedItems = array();
@ -151,8 +161,6 @@ class YamlFixture extends Object {
} }
} elseif($obj->has_one($fieldName)) { } elseif($obj->has_one($fieldName)) {
$obj->{$fieldName . 'ID'} = $this->parseFixtureVal($fieldVal); $obj->{$fieldName . 'ID'} = $this->parseFixtureVal($fieldVal);
} else {
$obj->$fieldName = $this->parseFixtureVal($fieldVal);
} }
} }
$obj->write(); $obj->write();