Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Versionable] Added support of M-N relations (isCrossRef)

  • Loading branch information...
commit ee62f0eb527d520b7a8ed8136e76abb348de4129 1 parent 3849529
@willdurand willdurand authored
View
49 generator/lib/behavior/versionable/VersionableBehavior.php
@@ -187,6 +187,23 @@ public function addForeignKeyVersionColumns()
));
}
}
+ foreach ($this->getVersionableCrossForeignKeys() as $fk) {
+ $fkTableName = $fk->getForeignTable()->getName();
+ $fkIdsColumnName = $fkTableName . '_ids';
+ if (!$versionTable->containsColumn($fkIdsColumnName)) {
+ $versionTable->addColumn(array(
+ 'name' => $fkIdsColumnName,
+ 'type' => 'ARRAY'
+ ));
+ }
+ $fkVersionsColumnName = $fkTableName . '_versions';
+ if (!$versionTable->containsColumn($fkVersionsColumnName)) {
+ $versionTable->addColumn(array(
+ 'name' => $fkVersionsColumnName,
+ 'type' => 'ARRAY'
+ ));
+ }
+ }
}
public function getVersionTable()
@@ -213,6 +230,22 @@ public function getVersionableFks()
return $versionableFKs;
}
+ public function getVersionableCrossForeignKeys($withRefFks = false)
+ {
+ $versionableFks = array();
+ if ($this->getTable()->hasCrossForeignKeys()) {
+ foreach ($this->getTable()->getCrossFks() as $fkList) {
+ list($refFk, $crossFk) = $fkList;
+
+ if ($crossFk->getForeignTable()->hasBehavior($this->getName()) && ! $crossFk->isComposite()) {
+ $versionableFks[] = $withRefFks ? $fkList : $crossFk;
+ }
+ }
+ }
+
+ return $versionableFks;
+ }
+
public function getVersionableReferrers()
{
$versionableReferrers = array();
@@ -235,6 +268,14 @@ public function getReferrerIdsColumn(ForeignKey $fk)
return $this->versionTable->getColumn($fkIdsColumnName);
}
+ public function getCrossForeignKeyIdsColumn(ForeignKey $fk)
+ {
+ $fkTableName = $fk->getForeignTable()->getName();
+ $fkIdsColumnName = $fkTableName . '_ids';
+
+ return $this->versionTable->getColumn($fkIdsColumnName);
+ }
+
public function getReferrerVersionsColumn(ForeignKey $fk)
{
$fkTableName = $fk->getTable()->getName();
@@ -243,6 +284,14 @@ public function getReferrerVersionsColumn(ForeignKey $fk)
return $this->versionTable->getColumn($fkIdsColumnName);
}
+ public function getCrossForeignKeyVersionsColumn(ForeignKey $fk)
+ {
+ $fkTableName = $fk->getForeignTable()->getName();
+ $fkIdsColumnName = $fkTableName . '_versions';
+
+ return $this->versionTable->getColumn($fkIdsColumnName);
+ }
+
public function getObjectBuilderModifier()
{
if (is_null($this->objectBuilderModifier)) {
View
22 generator/lib/behavior/versionable/VersionableBehaviorObjectBuilderModifier.php
@@ -184,16 +184,17 @@ public function isVersioningNecessary(\$con = null)
if (\$this->alreadyInSave) {
return false;
}
+
if ({$peerClass}::isVersioningEnabled() && (\$this->isNew() || \$this->isModified() || \$this->isDeleted())) {
return true;
}";
foreach ($this->behavior->getVersionableFks() as $fk) {
$fkGetter = $this->builder->getFKPhpNameAffix($fk, $plural = false);
$script .= "
+
if (null !== (\$object = \$this->get{$fkGetter}(\$con)) && \$object->isVersioningNecessary(\$con)) {
return true;
- }
-";
+ }";
}
foreach ($this->behavior->getVersionableReferrers() as $fk) {
$fkGetter = $this->builder->getRefFKPhpNameAffix($fk, $plural = true);
@@ -210,6 +211,7 @@ public function isVersioningNecessary(\$con = null)
\$this->alreadyInSave = false;
";
}
+
$script .= "
return false;
@@ -232,12 +234,15 @@ protected function addAddVersion(&$script)
public function addVersion(\$con = null)
{
\$version = new {$versionARClassname}();";
+
foreach ($this->table->getColumns() as $col) {
$script .= "
\$version->set" . $col->getPhpName() . "(\$this->get" . $col->getPhpName() . "());";
}
+
$script .= "
\$version->set{$this->table->getPhpName()}(\$this);";
+
foreach ($this->behavior->getVersionableFks() as $fk) {
$fkGetter = $this->builder->getFKPhpNameAffix($fk, $plural = false);
$fkVersionColumnName = $fk->getLocalColumnName() . '_version';
@@ -247,6 +252,7 @@ public function addVersion(\$con = null)
\$version->set{$fkVersionColumnPhpName}(\$related->getVersion());
}";
}
+
foreach ($this->behavior->getVersionableReferrers() as $fk) {
$fkGetter = $this->builder->getRefFKPhpNameAffix($fk, $plural = true);
$idsColumn = $this->behavior->getReferrerIdsColumn($fk);
@@ -257,6 +263,18 @@ public function addVersion(\$con = null)
\$version->set{$versionsColumn->getPhpName()}(array_values(\$relateds));
}";
}
+
+ foreach ($this->behavior->getVersionableCrossForeignKeys() as $fk) {
+ $fkGetter = $this->builder->getFKPhpNameAffix($fk, $plural = true);
+ $idsColumn = $this->behavior->getCrossForeignKeyIdsColumn($fk);
+ $versionsColumn = $this->behavior->getCrossForeignKeyVersionsColumn($fk);
+ $script .= "
+ if (\$relateds = \$this->get{$fkGetter}(\$con)->toKeyValue('PrimaryKey', 'Version')) {
+ \$version->set{$idsColumn->getPhpName()}(array_keys(\$relateds));
+ \$version->set{$versionsColumn->getPhpName()}(array_values(\$relateds));
+ }";
+ }
+
$script .= "
\$version->save(\$con);
View
52 test/testsuite/generator/behavior/versionable/VersionableBehaviorObjectBuilderModifierTest.php
@@ -66,6 +66,29 @@ public function setUp()
<behavior name="versionable" />
</table>
+ <table name="versionable_post">
+ <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+ <column name="title" type="VARCHAR" size="255" />
+ <behavior name="versionable" />
+ </table>
+
+ <table name="versionable_tag">
+ <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+ <column name="name" type="VARCHAR" size="255" />
+ <behavior name="versionable" />
+ </table>
+
+ <table name="versionable_post_tag" isCrossRef="true">
+ <column name="post_id" primaryKey="true" type="INTEGER" />
+ <column name="tag_id" primaryKey="true" type="INTEGER" />
+
+ <foreign-key foreignTable="versionable_post">
+ <reference local="post_id" foreign="id" />
+ </foreign-key>
+ <foreign-key foreignTable="versionable_tag">
+ <reference local="tag_id" foreign="id" />
+ </foreign-key>
+ </table>
</database>
EOF;
PropelQuickBuilder::buildSchema($schema);
@@ -876,8 +899,8 @@ public function testCustomIdName()
$b1->save();
}
- public function testWithInheritance()
- {
+ public function testWithInheritance()
+ {
$b1 = new VersionableBehaviorTest8Foo();
$b1->save();
@@ -886,5 +909,28 @@ public function testWithInheritance()
$object = $b1->getOneVersion($b1->getVersion());
$this->assertTrue($object instanceof Versionablebehaviortest8Version);
- }
+ }
+
+ public function testManyToManyWithIsCrossRef()
+ {
+ $t1 = new VersionableTag();
+ $t1->setName('foo');
+
+ $post = new VersionablePost();
+ $post->setTitle('Hello, World');
+ $post->addVersionableTag($t1);
+
+ $post->save();
+
+ $this->assertFalse($post->isNew());
+ $this->assertEquals(1, $post->getVersion());
+ $this->assertEquals(array(1), $post->getOneVersion(1)->getVersionableTagIds());
+
+ $post->setTitle('bar');
+ $post->removeVersionableTag($t1);
+ $post->save();
+
+ $this->assertEquals(2, $post->getVersion());
+ $this->assertEquals(array(), $post->getOneVersion(2)->getVersionableTagIds());
+ }
}
View
1  test/testsuite/generator/behavior/versionable/VersionableBehaviorTest.php
@@ -463,5 +463,4 @@ public function testDatabaseLevelBehavior()
EOF;
$this->assertContains($expected, $builder->getSQL());
}
-
}
Please sign in to comment.
Something went wrong with that request. Please try again.