DataLoader: allow to set foreign key for a table not included in fixtures
This commit is contained in:
parent
e729612866
commit
e80e31858d
|
@ -144,7 +144,7 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
|
||||||
$tableMap = $this->dbMap->getTable(constant(constant($class.'::TABLE_MAP').'::TABLE_NAME'));
|
$tableMap = $this->dbMap->getTable(constant(constant($class.'::TABLE_MAP').'::TABLE_NAME'));
|
||||||
$column_names = $tableMap->getFieldnames(TableMap::TYPE_PHPNAME);
|
$column_names = $tableMap->getFieldnames(TableMap::TYPE_PHPNAME);
|
||||||
|
|
||||||
foreach ($datas as $key => $data) {
|
foreach ($datas as $key => $values) {
|
||||||
// create a new entry in the database
|
// create a new entry in the database
|
||||||
if (!class_exists($class)) {
|
if (!class_exists($class)) {
|
||||||
throw new \InvalidArgumentException(sprintf('Unknown class "%s".', $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));
|
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)) {
|
if (is_array($value) && 's' === substr($name, -1)) {
|
||||||
try {
|
try {
|
||||||
// many to many relationship
|
// many to many relationship
|
||||||
|
@ -205,14 +205,19 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
|
||||||
|
|
||||||
if ($column->isForeignKey() && null !== $value) {
|
if ($column->isForeignKey() && null !== $value) {
|
||||||
$relatedTable = $this->dbMap->getTable($column->getRelatedTableName());
|
$relatedTable = $this->dbMap->getTable($column->getRelatedTableName());
|
||||||
if (!isset($this->object_references[$this->cleanObjectRef($relatedTable->getClassname().'_'.$value)])) {
|
if (isset($this->object_references[$this->cleanObjectRef($relatedTable->getClassname().'_'.$value)])) {
|
||||||
throw new \InvalidArgumentException(
|
$value = $this
|
||||||
sprintf('The object "%s" from class "%s" is not defined in your data file.', $value, $relatedTable->getClassname())
|
->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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,38 @@ YAML;
|
||||||
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor', $book->getCoolBookAuthor());
|
$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()
|
public function testLoadSelfReferencing()
|
||||||
{
|
{
|
||||||
$fixtures = <<<YAML
|
$fixtures = <<<YAML
|
||||||
|
|
Loading…
Reference in a new issue