Permalink
Browse files

Merge pull request #223 from havvg/feature/data-loader-fkpk

add support for delegated fixtures in 1:1 relation
  • Loading branch information...
2 parents 6b2913c + 675e711 commit 4b9747d03bf3f64ce80aa96b0d2b4564586fdb09 @willdurand willdurand committed Apr 9, 2013
Showing with 57 additions and 2 deletions.
  1. +5 −2 DataFixtures/Loader/AbstractDataLoader.php
  2. +52 −0 Tests/DataFixtures/Loader/YamlDataLoaderTest.php
View
7 DataFixtures/Loader/AbstractDataLoader.php
@@ -198,8 +198,11 @@ protected function loadDataFromArray($data = null)
// foreign key?
if ($isARealColumn) {
- // self referencing entry
- if ($column->isPrimaryKey() && null !== $value) {
+ /*
+ * A column, which is a PrimaryKey (self referencing, e.g. versionable behavior),
+ * but which is not a ForeignKey (e.g. delegatable behavior on 1:1 relation).
+ */
+ if ($column->isPrimaryKey() && null !== $value && !$column->isForeignKey()) {
if (isset($this->object_references[$class.'_'.$value])) {
$obj = $this->object_references[$class.'_'.$value];
View
52 Tests/DataFixtures/Loader/YamlDataLoaderTest.php
@@ -411,4 +411,56 @@ public function testLoadArrayToObjectType()
$this->assertInstanceOf('\Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlBookWithObject', $book);
$this->assertEquals(array('opt1' => 2012, 'opt2' => 140, 'inner' => array('subOpt' => 123)), $book->getOptions());
}
+
+ public function testLoadDelegatedOnPrimaryKey()
+ {
+ $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="yaml_delegate_on_primary_key_person" phpName="YamlDelegateOnPrimaryKeyPerson">
+ <column name="id" type="integer" primaryKey="true" autoIncrement="true" />
+ <column name="name" type="varchar" size="255" />
+ </table>
+
+ <table name="yaml_delegate_on_primary_key_author" phpName="YamlDelegateOnPrimaryKeyAuthor">
+ <column name="id" type="integer" primaryKey="true" autoIncrement="false" />
+ <column name="count_books" type="integer" defaultValue="0" required="true" />
+
+ <behavior name="delegate">
+ <parameter name="to" value="yaml_delegate_on_primary_key_person" />
+ </behavior>
+
+ <foreign-key foreignTable="yaml_delegate_on_primary_key_person" onDelete="RESTRICT" onUpdate="CASCADE">
+ <reference local="id" foreign="id" />
+ </foreign-key>
+ </table>
+</database>
+XML;
+
+ $fixtures = <<<YAML
+Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyPerson:
+ yaml_delegate_on_primary_key_person_1:
+ name: "Some Persons Name"
+
+Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyAuthor:
+ yaml_delegate_on_primary_key_author_1:
+ id: yaml_delegate_on_primary_key_person_1
+ count_books: 7
+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\YamlDelegateOnPrimaryKeyAuthorPeer::doSelect(new \Criteria(), $con);
+ $this->assertCount(1, $authors);
+
+ $author = $authors[0];
+ $person = $author->getYamlDelegateOnPrimaryKeyPerson();
+ $this->assertInstanceOf('Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyPerson', $person);
+ }
}

0 comments on commit 4b9747d

Please sign in to comment.