Merge pull request #198 from jpetitcolas/1.1
Use only child object when filling fixtures with relationship and concrete_inheritance
This commit is contained in:
commit
26596d1df4
|
@ -92,18 +92,35 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
|
|||
if (in_array($class, $this->deletedClasses)) {
|
||||
continue;
|
||||
}
|
||||
$this->deleteClassData($class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check that peer class exists before calling doDeleteAll()
|
||||
$peerClass = constant($class.'::PEER');
|
||||
if (!class_exists($peerClass)) {
|
||||
throw new \InvalidArgumentException(sprintf('Unknown class "%sPeer".', $class));
|
||||
}
|
||||
/**
|
||||
* Delete data for a given class, and for its ancestors (if any).
|
||||
*
|
||||
* @param string $class Class name to delete
|
||||
*/
|
||||
protected function deleteClassData($class)
|
||||
{
|
||||
// Check that peer class exists before calling doDeleteAll()
|
||||
$peerClass = constant($class.'::PEER');
|
||||
if (!class_exists($peerClass)) {
|
||||
throw new \InvalidArgumentException(sprintf('Unknown class "%sPeer".', $class));
|
||||
}
|
||||
|
||||
// bypass the soft_delete behavior if enabled
|
||||
$deleteMethod = method_exists($peerClass, 'doForceDeleteAll') ? 'doForceDeleteAll' : 'doDeleteAll';
|
||||
call_user_func(array($peerClass, $deleteMethod), $this->con);
|
||||
// bypass the soft_delete behavior if enabled
|
||||
$deleteMethod = method_exists($peerClass, 'doForceDeleteAll') ? 'doForceDeleteAll' : 'doDeleteAll';
|
||||
call_user_func(array($peerClass, $deleteMethod), $this->con);
|
||||
|
||||
$this->deletedClasses[] = $class;
|
||||
$this->deletedClasses[] = $class;
|
||||
|
||||
// Remove ancestors data
|
||||
if(false !== ($parentClass = get_parent_class(get_parent_class($class)))) {
|
||||
$reflectionClass = new \ReflectionClass($parentClass);
|
||||
if(!$reflectionClass->isAbstract()) {
|
||||
$this->deleteClassData($parentClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -212,14 +229,39 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
|
|||
|
||||
$obj->save($this->con);
|
||||
|
||||
// save the object for future reference
|
||||
if (method_exists($obj, 'getPrimaryKey')) {
|
||||
$this->object_references[$class.'_'.$key] = $obj;
|
||||
}
|
||||
$this->saveParentReference($class, $key, $obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a reference to the specified object (and its ancestors) before loading them.
|
||||
*
|
||||
* @param string $class Class name of passed object
|
||||
* @param string $key Key identifying specified object
|
||||
* @param BaseObject $obj A Propel object
|
||||
*/
|
||||
protected function saveParentReference($class, $key, &$obj)
|
||||
{
|
||||
if(method_exists($obj, 'getPrimaryKey')) {
|
||||
|
||||
$this->object_references[$class.'_'.$key] = $obj;
|
||||
|
||||
// Get parent (schema ancestor) of parent (Propel base class) in case of inheritance
|
||||
if(false !== ($parentClass = get_parent_class(get_parent_class($class)))) {
|
||||
|
||||
$reflectionClass = new \ReflectionClass($parentClass);
|
||||
if(!$reflectionClass->isAbstract()) {
|
||||
$parentObj = new $parentClass;
|
||||
$parentObj->fromArray($obj->toArray());
|
||||
$this->saveParentReference($parentClass, $key, $parentObj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads many to many objects.
|
||||
*
|
||||
|
|
|
@ -321,4 +321,61 @@ YAML;
|
|||
$this->assertNotEquals('null', strtolower($book->getDescription()));
|
||||
$this->assertRegexp('#[\w ]+#', $book->getDescription());
|
||||
}
|
||||
|
||||
public function testLoadWithInheritedRelationship()
|
||||
{
|
||||
$schema = <<<XML
|
||||
<database name="default" package="vendor.bundles.Propel.PropelBundle.Tests.Fixtures.DataFixtures.Loader" namespace="Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader" defaultIdMethod="native">
|
||||
|
||||
<table name="table_book_inherited_relationship" phpName="YamlInheritedRelationshipBook">
|
||||
<column name="id" type="integer" primaryKey="true" autoIncrement="true" />
|
||||
<column name="name" type="varchar" size="255" />
|
||||
<column name="author_id" type="integer" required="true" />
|
||||
<foreign-key foreignTable="table_author_inherited_relationship" phpName="Author">
|
||||
<reference local="author_id" foreign="id" />
|
||||
</foreign-key>
|
||||
</table>
|
||||
|
||||
<table name="table_author_inherited_relationship" phpName="YamlInheritedRelationshipAuthor">
|
||||
<column name="id" type="integer" primaryKey="true" autoIncrement="true" />
|
||||
<column name="name" type="varchar" size="255" />
|
||||
</table>
|
||||
|
||||
<table name="table_nobelized_author_inherited_relationship" phpName="YamlInheritedRelationshipNobelizedAuthor">
|
||||
<column name="nobel_year" type="integer" />
|
||||
<behavior name="concrete_inheritance">
|
||||
<parameter name="extends" value="table_author_inherited_relationship" />
|
||||
</behavior>
|
||||
</table>
|
||||
|
||||
</database>
|
||||
XML;
|
||||
|
||||
$fixtures = <<<YAML
|
||||
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipNobelizedAuthor:
|
||||
NobelizedAuthor_1:
|
||||
nobel_year: 2012
|
||||
|
||||
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipBook:
|
||||
Book_1:
|
||||
name: 'Supplice du santal'
|
||||
author_id: NobelizedAuthor_1
|
||||
YAML;
|
||||
|
||||
$filename = $this->getTempFile($fixtures);
|
||||
|
||||
$builder = new \PropelQuickBuilder();
|
||||
$builder->setSchema($schema);
|
||||
$con = $builder->build();
|
||||
|
||||
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader');
|
||||
$loader->load(array($filename), 'default');
|
||||
|
||||
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipBookPeer::doSelect(new \Criteria(), $con);
|
||||
$this->assertCount(1, $books);
|
||||
|
||||
$book = $books[0];
|
||||
$author = $book->getAuthor();
|
||||
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipAuthor', $author);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue