Permalink
Browse files

Fix #282 versionable behavior FK in generated object classes

Fix #282 by replacing foreign key references with PrimaryKey
  • Loading branch information...
1 parent 14a1702 commit 5ebefc38df28766b397dd0436cf72d816379b3b9 @aamsellem aamsellem committed with willdurand Feb 6, 2012
@@ -244,7 +244,7 @@ public function addVersion(\$con = null)
$idsColumn = $this->behavior->getReferrerIdsColumn($fk);
$versionsColumn = $this->behavior->getReferrerVersionsColumn($fk);
$script .= "
- if (\$relateds = \$this->get{$fkGetter}(\$con)->toKeyValue('{$fk->getForeignColumn()->getPhpName()}', 'Version')) {
+ if (\$relateds = \$this->get{$fkGetter}(\$con)->toKeyValue('{$fk->getTable()->getFirstPrimaryKeyColumn()->getPhpName()}', 'Version')) {
\$version->set{$idsColumn->getPhpName()}(array_keys(\$relateds));
\$version->set{$versionsColumn->getPhpName()}(array_values(\$relateds));
}";
@@ -354,7 +354,7 @@ public function populateFromVersion(\$version, \$con = null, &\$loadedObjects =
$this->builder->declareClassFromBuilder($relatedVersionPeerBuilder);
$relatedVersionPeerClassname = $relatedVersionPeerBuilder->getClassname();
$relatedClassname = $this->builder->getNewStubObjectBuilder($foreignTable)->getClassname();
- $fkColumn = $fk->getForeignColumn();
+ $fkColumn = $foreignVersionTable->getFirstPrimaryKeyColumn();
$fkVersionColumn = $foreignVersionTable->getColumn($this->behavior->getParameter('version_column'));
$script .= "
if (\$fkValues = \$version->get{$fkColumnIds->getPhpName()}()) {
@@ -70,38 +70,61 @@ public function setUp()
EOF;
PropelQuickBuilder::buildSchema($schema);
}
+
if (!class_exists('VersionableBehaviorTest6')) {
$schema2 = <<<EOF
-<database name="versionable_behavior_test_2" defaultPhpNamingMethod="nochange">
- <table name="VersionableBehaviorTest6">
- <column name="Id" primaryKey="true" type="INTEGER" autoIncrement="true" />
- <column name="FooBar" type="VARCHAR" size="100" />
- <behavior name="versionable">
- <parameter name="log_created_at" value="true" />
- <parameter name="log_created_by" value="true" />
- <parameter name="log_comment" value="true" />
- </behavior>
- </table>
-
- <table name="VersionableBehaviorTest7">
- <column name="Id" primaryKey="true" type="INTEGER" autoIncrement="true" />
- <column name="FooBar" type="VARCHAR" size="100" />
-
- <column name="Style" type="ENUM" valueSet="novel, essay, poetry" />
-
- <behavior name="versionable">
- <parameter name="log_created_at" value="true" />
- <parameter name="log_created_by" value="true" />
- <parameter name="log_comment" value="true" />
-
- <parameter name="version_created_by_column" value="VersionCreatedBy" />
- <parameter name="version_created_at_column" value="VersionCreatedAt" />
- <parameter name="version_comment_column" value="MyComment" />
- </behavior>
- </table>
+ <database name="versionable_behavior_test_2" defaultPhpNamingMethod="nochange">
+ <table name="VersionableBehaviorTest6">
+ <column name="Id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+ <column name="FooBar" type="VARCHAR" size="100" />
+ <behavior name="versionable">
+ <parameter name="log_created_at" value="true" />
+ <parameter name="log_created_by" value="true" />
+ <parameter name="log_comment" value="true" />
+ </behavior>
+ </table>
+
+ <table name="VersionableBehaviorTest7">
+ <column name="Id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+ <column name="FooBar" type="VARCHAR" size="100" />
+
+ <column name="Style" type="ENUM" valueSet="novel, essay, poetry" />
+
+ <behavior name="versionable">
+ <parameter name="log_created_at" value="true" />
+ <parameter name="log_created_by" value="true" />
+ <parameter name="log_comment" value="true" />
+
+ <parameter name="version_created_by_column" value="VersionCreatedBy" />
+ <parameter name="version_created_at_column" value="VersionCreatedAt" />
+ <parameter name="version_comment_column" value="MyComment" />
+ </behavior>
+ </table>
EOF;
PropelQuickBuilder::buildSchema($schema2);
}
+
+ if (!class_exists('VersionableBehaviorTest8')) {
+ $schema3 = <<<EOF
+ <database name="versionable_behavior_test_3">
+ <table name="VersionableBehaviorTest8">
+ <column name="alter_id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+ <column name="FooBar" type="VARCHAR" size="100" />
+ <behavior name="versionable" />
+ </table>
+
+ <table name="VersionableBehaviorTest9">
+ <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+ <column name="foo" type="VARCHAR" size="100" />
+ <column name="foreign_id" type="INTEGER" />
+ <foreign-key foreignTable="VersionableBehaviorTest8">
+ <reference local="foreign_id" foreign="alter_id" />
+ </foreign-key>
+ <behavior name="versionable" />
+ </table>
+EOF;
+ PropelQuickBuilder::buildSchema($schema3);
+ }
}
public function testGetVersionExists()
@@ -474,7 +497,7 @@ public function testIsVersioningNecessary()
$this->assertTrue($a->isVersioningNecessary());
$a->save();
$this->assertFalse($a->isVersioningNecessary());
- $b2->setFoo('World !');
+ $b2->setFoo('World !');
$this->assertTrue($b2->isVersioningNecessary());
$this->assertTrue($a->isVersioningNecessary());
$a->save();
@@ -697,4 +720,18 @@ public function testEnumField()
$this->assertEquals('novel', $o->getOneVersion(1)->getStyle(), 'First version is a novel');
$this->assertEquals('essay', $o->getOneVersion(2)->getStyle(), 'Second version is an essay');
}
+
+ public function testCustomIdName()
+ {
+ $b1 = new VersionableBehaviorTest8();
+ $b2 = new VersionableBehaviorTest9();
+ $b1->save();
+ $b2->setVersionableBehaviorTest8($b1);
+ $b2->save();
+ $b2->setFoo('test2');
+ $b2->save();
+ $b1->setFoobar('test1');
+ $b1->save();
+ }
+
}

0 comments on commit 5ebefc3

Please sign in to comment.