Permalink
Browse files

Merge pull request #114 from havvg/feature/dataloader-self-reference

allow self reference in DataLoader
  • Loading branch information...
2 parents d8f2fc5 + ca68ea6 commit 49598d1aa9316cfbe91558bbac0c3775582bafd6 @willdurand willdurand committed Mar 5, 2012
Showing with 36 additions and 0 deletions.
  1. +9 −0 DataFixtures/Loader/AbstractDataLoader.php
  2. +27 −0 Tests/DataFixtures/Loader/YamlDataLoaderTest.php
@@ -167,6 +167,15 @@ protected function loadDataFromArray($data = null)
// foreign key?
if ($isARealColumn) {
+ // self referencing entry
+ if ($column->isPrimaryKey() && null !== $value) {
+ if (isset($this->object_references[$class.'_'.$value])) {
+ $obj = $this->object_references[$class.'_'.$value];
+
+ continue;
+ }
+ }
+
if ($column->isForeignKey() && null !== $value) {
$relatedTable = $this->dbMap->getTable($column->getRelatedTableName());
if (!isset($this->object_references[$relatedTable->getClassname().'_'.$value])) {
@@ -108,4 +108,31 @@ public function testYamlLoadManyToMany()
$this->assertCount(1, $bookAuthors);
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookAuthor', $bookAuthors[0]);
}
+
+ public function testLoadSelfReferencing()
+ {
+ $fixtures = <<<YAML
+Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor:
+ BookAuthor_1:
+ id: '1'
+ name: 'to be announced'
+ BookAuthor_2:
+ id: BookAuthor_1
+ name: 'A famous one'
+
+YAML;
+ $filename = $this->getTempFile($fixtures);
+
+ $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
+ $loader->load(array($filename), 'default');
+
+ $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
+ $this->assertCount(0, $books);
+
+ $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthorPeer::doSelect(new \Criteria(), $this->con);
+ $this->assertCount(1, $authors);
+
+ $author = $authors[0];
+ $this->assertEquals('A famous one', $author->getName());
+ }
}

0 comments on commit 49598d1

Please sign in to comment.