Skip to content

Commit

Permalink
feat(DataLoader): allow to set foreign key for a table not included i…
Browse files Browse the repository at this point in the history
…n fixture
  • Loading branch information
aurnot committed Jun 15, 2016
1 parent db2ef84 commit 8920f80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
15 changes: 8 additions & 7 deletions DataFixtures/Loader/AbstractDataLoader.php
Expand Up @@ -151,7 +151,7 @@ protected function loadDataFromArray($data = null)
continue;
}

foreach ($datas as $key => $data) {
foreach ($datas as $key => $values) {
// create a new entry in the database
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Unknown class "%s".', $class));
Expand All @@ -165,11 +165,11 @@ protected function loadDataFromArray($data = null)
);
}

if (!is_array($data)) {
if (!is_array($values)) {
throw new \InvalidArgumentException(sprintf('You must give a name for each fixture data entry (class %s).', $class));
}

foreach ($data as $name => $value) {
foreach ($values as $name => $value) {
if (is_array($value) && 's' === substr($name, -1)) {
try {
// many to many relationship
Expand Down Expand Up @@ -212,14 +212,15 @@ protected function loadDataFromArray($data = null)

if ($column->isForeignKey() && null !== $value) {
$relatedTable = $this->dbMap->getTable($column->getRelatedTableName());
if (!isset($this->object_references[$relatedTable->getClassname().'_'.$value])) {
if (isset($this->object_references[$relatedTable->getClassname().'_'.$value])) {
$value = $this
->object_references[$relatedTable->getClassname().'_'.$value]
->getByName($column->getRelatedName(), BasePeer::TYPE_COLNAME);
} else if (isset($data[$relatedTable->getClassName()])) {
throw new \InvalidArgumentException(
sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getClassname())
);
}
$value = $this
->object_references[$relatedTable->getClassname().'_'.$value]
->getByName($column->getRelatedName(), BasePeer::TYPE_COLNAME);
}
}

Expand Down
26 changes: 26 additions & 0 deletions Tests/DataFixtures/Loader/YamlDataLoaderTest.php
Expand Up @@ -45,6 +45,32 @@ public function testYamlLoadOneToMany()
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor', $book->getBookAuthor());
}

public function testYamlLoadOneToManyExternalReference()
{
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
$fixtures = <<<YAML
\Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor:
BookAuthor_1:
id: '1'
name: 'A famous one'
YAML;
$filename = $this->getTempFile($fixtures);
$loader->load(array($filename), 'default');
$fixtures = <<<YAML
\Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book:
Book_1:
id: '1'
name: 'An important one'
author_id: 1
YAML;
$filename = $this->getTempFile($fixtures);
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookQuery::create()->find($this->con);
$this->assertCount(1, $books);
$book = $books[0];
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor', $book->getBookAuthor());
}

public function testYamlLoadManyToMany()
{
$schema = <<<XML
Expand Down

0 comments on commit 8920f80

Please sign in to comment.