diff --git a/DataFixtures/Loader/AbstractDataLoader.php b/DataFixtures/Loader/AbstractDataLoader.php index 938c0bb..f8b57e7 100644 --- a/DataFixtures/Loader/AbstractDataLoader.php +++ b/DataFixtures/Loader/AbstractDataLoader.php @@ -144,7 +144,7 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa $tableMap = $this->dbMap->getTable(constant(constant($class.'::TABLE_MAP').'::TABLE_NAME')); $column_names = $tableMap->getFieldnames(TableMap::TYPE_PHPNAME); - 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)); @@ -158,11 +158,11 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa ); } - 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 @@ -205,14 +205,19 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa if ($column->isForeignKey() && null !== $value) { $relatedTable = $this->dbMap->getTable($column->getRelatedTableName()); - if (!isset($this->object_references[$this->cleanObjectRef($relatedTable->getClassname().'_'.$value)])) { - throw new \InvalidArgumentException( - sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getClassname()) - ); + if (isset($this->object_references[$this->cleanObjectRef($relatedTable->getClassname().'_'.$value)])) { + $value = $this + ->object_references[$this->cleanObjectRef($relatedTable->getClassname().'_'.$value)] + ->getByName($column->getRelatedName(), TableMap::TYPE_COLNAME); + } else { + $relatedClass = $this->cleanObjectRef($relatedTable->getClassName()); + if (isset($data[$relatedClass]) || isset($data['\\' . $relatedClass])) { + throw new \InvalidArgumentException( + sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getClassname()) + ); + + } } - $value = $this - ->object_references[$this->cleanObjectRef($relatedTable->getClassname().'_'.$value)] - ->getByName($column->getRelatedName(), TableMap::TYPE_COLNAME); } } diff --git a/Tests/DataFixtures/Loader/YamlDataLoaderTest.php b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php index 2196d93..453eac3 100644 --- a/Tests/DataFixtures/Loader/YamlDataLoaderTest.php +++ b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php @@ -47,6 +47,38 @@ YAML; $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor', $book->getCoolBookAuthor()); } + public function testYamlLoadOneToManyExternalReference() + { + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array()); + + $fixtures = <<getTempFile($fixtures); + $loader->load(array($filename), 'default'); + + $fixtures = <<getTempFile($fixtures); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con); + $this->assertCount(1, $books); + + $book = $books[0]; + $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor', $book->getCoolBookAuthor()); + } + public function testLoadSelfReferencing() { $fixtures = <<