Merge pull request #256 from pimpreneil/1.2

Add inheritance support for fixtures with many to many relationship
This commit is contained in:
Toni Uebernickel 2014-01-06 22:28:58 -08:00
parent b16659a28c
commit 8d60937f84
2 changed files with 96 additions and 4 deletions

View file

@ -257,8 +257,7 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
$reflectionClass = new \ReflectionClass($parentClass);
if (!$reflectionClass->isAbstract()) {
$parentObj = new $parentClass;
$parentObj->fromArray($obj->toArray());
$parentObj = $obj->getParentOrCreate();
$this->saveParentReference($parentClass, $key, $parentObj);
}
@ -267,6 +266,24 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
}
}
/**
* Retrieve all the parent classes of a given class (for the inheritance).
*
* @param string $class Class name of the current object
*/
protected function getInheritedClasses($class) {
$reflectionClass = new \ReflectionClass($class);
$classes = array();
while (!$reflectionClass->isAbstract()) {
$classes[] = constant(constant($class.'::PEER').'::TABLE_NAME');
$class = get_parent_class(get_parent_class($class));
$reflectionClass = new \ReflectionClass($class);
}
return $classes;
}
/**
* Loads many to many objects.
*
@ -278,11 +295,11 @@ abstract class AbstractDataLoader extends AbstractDataHandler implements DataLoa
{
$middleTable = $this->dbMap->getTable($middleTableName);
$middleClass = $middleTable->getClassname();
$tableName = constant(constant(get_class($obj).'::PEER').'::TABLE_NAME');
$inheritedClasses = $this->getInheritedClasses(get_class($obj));
foreach ($middleTable->getColumns() as $column) {
if ($column->isForeignKey()) {
if ($tableName !== $column->getRelatedTableName()) {
if (!in_array($column->getRelatedTableName(), $inheritedClasses)) {
$relatedClass = $this->dbMap->getTable($column->getRelatedTableName())->getClassname();
$relatedSetter = 'set' . $column->getRelation()->getName();
} else {

View file

@ -379,6 +379,81 @@ YAML;
$this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipAuthor', $author);
}
public function testLoadWithInheritedManyToManyRelationship()
{
$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_m2m_relationship" phpName="YamlInheritedM2MRelationshipBook">
<column name="id" type="integer" primaryKey="true" autoIncrement="true" />
<column name="name" type="varchar" size="255" />
</table>
<table name="table_history_book_inherited_m2m_relationship" phpName="YamlInheritedM2MRelationshipHistoryBook">
<behavior name="concrete_inheritance">
<parameter name="extends" value="table_book_inherited_m2m_relationship" />
</behavior>
</table>
<table name="table_author_inherited_m2m_relationship" phpName="YamlInheritedM2MRelationshipAuthor">
<column name="id" type="integer" primaryKey="true" autoIncrement="true" />
<column name="name" type="varchar" size="255" />
</table>
<table name="table_nobelized_author_inherited_m2m_relationship" phpName="YamlInheritedM2MRelationshipNobelizedAuthor">
<column name="nobel_year" type="integer" />
<behavior name="concrete_inheritance">
<parameter name="extends" value="table_author_inherited_m2m_relationship" />
</behavior>
</table>
<table name="table_book_author_inherited_m2m_relationship" phpName="YamlInheritedM2MRelationshipBookAuthor" isCrossRef="true">
<column name="author_id" type="integer" primaryKey="true" />
<column name="book_id" type="integer" primaryKey="true" />
<foreign-key foreignTable="table_author_inherited_m2m_relationship" phpName="Author">
<reference local="author_id" foreign="id" />
</foreign-key>
<foreign-key foreignTable="table_book_inherited_m2m_relationship" phpName="Book">
<reference local="book_id" foreign="id" />
</foreign-key>
</table>
</database>
XML;
$fixtures = <<<YAML
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedM2MRelationshipBook:
Book_1:
name: 'Supplice du santal'
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedM2MRelationshipHistoryBook:
Book_2:
name: 'Qiushui'
Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedM2MRelationshipNobelizedAuthor:
NobelizedAuthor_1:
nobel_year: 2012
table_book_author_inherited_m2m_relationships: [Book_1, Book_2]
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');
$authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedM2MRelationshipNobelizedAuthorPeer::doSelect(new \Criteria(), $con);
$this->assertCount(1, $authors);
$author = $authors[0];
$books = $author->getBooks();
$this->assertCount(2, $books);
}
public function testLoadArrayToObjectType()
{
$schema = <<<XML