Merge pull request #114 from havvg/feature/dataloader-self-reference
allow self reference in DataLoader
This commit is contained in:
commit
49598d1aa9
|
@ -167,6 +167,15 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
|
|||
|
||||
// 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 @@ YAML;
|
|||
$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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue