DataLoader: allow to set foreign key for a table not included in fixtures

This commit is contained in:
Gregor Harlan 2015-07-15 17:27:44 +02:00
parent e729612866
commit e80e31858d
2 changed files with 47 additions and 10 deletions

View file

@ -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);
}
}

View file

@ -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 = <<<YAML
\Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor:
CoolBookAuthor_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\CoolBook:
CoolBook_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\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 = <<<YAML