Skip to content

Commit

Permalink
allow self reference in DataLoader
Browse files Browse the repository at this point in the history
A self referencing entry will trigger an "UPDATE" rather than another "INSERT" with the new data.
  • Loading branch information
havvg committed Mar 5, 2012
1 parent eece448 commit ca68ea6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions DataFixtures/Loader/AbstractDataLoader.php
Expand Up @@ -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])) {
Expand Down
27 changes: 27 additions & 0 deletions Tests/DataFixtures/Loader/YamlDataLoaderTest.php
Expand Up @@ -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 ca68ea6

Please sign in to comment.