Permalink
Browse files

Merge pull request #256 from pimpreneil/1.2

Add inheritance support for fixtures with many to many relationship
  • Loading branch information...
2 parents ef95bcd + d6919b3 commit 2e544273769c71b200e7dfa31212791eec6a9aab @havvg havvg committed Jan 7, 2014
Showing with 96 additions and 4 deletions.
  1. +21 −4 DataFixtures/Loader/AbstractDataLoader.php
  2. +75 −0 Tests/DataFixtures/Loader/YamlDataLoaderTest.php
@@ -257,8 +257,7 @@ protected function saveParentReference($class, $key, &$obj)
$reflectionClass = new \ReflectionClass($parentClass);
if (!$reflectionClass->isAbstract()) {
- $parentObj = new $parentClass;
- $parentObj->fromArray($obj->toArray());
+ $parentObj = $obj->getParentOrCreate();
$this->saveParentReference($parentClass, $key, $parentObj);
}
@@ -268,6 +267,24 @@ protected function saveParentReference($class, $key, &$obj)
}
/**
+ * 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.
*
* @param BaseObject $obj A Propel object
@@ -278,11 +295,11 @@ protected function loadManyToMany($obj, $middleTableName, $values)
{
$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 {
@@ -379,6 +379,81 @@ public function testLoadWithInheritedRelationship()
$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

0 comments on commit 2e54427

Please sign in to comment.