Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
3 participants
Owner

willdurand commented May 29, 2012

No description provided.

This pull request passes (merged ee62f0e into 3b15493).

Member

havvg commented May 31, 2012

I didn't try yet, but does it support the case where only one of the models has attached the behavior?

Like for the test case only the post has versioning active and the tag does not, but you want to know when a tag was added/removed to/from the post.

Member

havvg commented Jun 6, 2012

This is the patch containing the test for the use-case described. It currently fails :)
I'm trying to figure out, how to alter the VersionableBehavior to get it working.

diff --git a/test/testsuite/generator/behavior/versionable/VersionableBehaviorObjectBuilderModifierTest.php b/test/te
index ff650ef..fa75a31 100644
--- a/test/testsuite/generator/behavior/versionable/VersionableBehaviorObjectBuilderModifierTest.php
+++ b/test/testsuite/generator/behavior/versionable/VersionableBehaviorObjectBuilderModifierTest.php
@@ -89,6 +89,29 @@ class VersionableBehaviorObjectBuilderModifierTest extends PHPUnit_Framework_Tes
             <reference local="tag_id" foreign="id" />
         </foreign-key>
     </table>
+
+    <table name="versionable_user">
+        <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+        <column name="username" type="VARCHAR" size="255" />
+        <behavior name="versionable" />
+    </table>
+
+    <table name="versionable_group">
+        <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
+        <column name="name" type="VARCHAR" size="255" />
+    </table>
+
+    <table name="versionable_user_group" isCrossRef="true">
+        <column name="user_id" primaryKey="true" type="INTEGER" />
+        <column name="group_id" primaryKey="true" type="INTEGER" />
+
+        <foreign-key foreignTable="versionable_user">
+            <reference local="user_id" foreign="id" />
+        </foreign-key>
+        <foreign-key foreignTable="versionable_group">
+            <reference local="group_id" foreign="id" />
+        </foreign-key>
+    </table>
 </database>
 EOF;
             PropelQuickBuilder::buildSchema($schema);
@@ -933,4 +956,27 @@ EOF;
         $this->assertEquals(2, $post->getVersion());
         $this->assertEquals(array(), $post->getOneVersion(2)->getVersionableTagIds());
     }
+
+    public function testManyToManyWithIsCrossRefOnlyOneHasVersionable()
+    {
+        $group = new VersionableGroup();
+        $group->setName('Awesome People');
+        $this->assertEquals(1, $group->save());
+
+        $user = new VersionableUser();
+        $user->setUsername('arthur-dent@marvin.net');
+        $this->assertEquals(1, $user->save());
+
+        $user->addVersionableGroup($group);
+        $this->assertTrue($user->isVersioningNecessary());
+        $this->assertGreaterThan(1, $user->save());
+        $this->assertEquals(2, $user->getVersion());
+        $this->assertEquals(array($group->getId()), $user->getOneVersion(2)->getVersionableGroupIds());
+
+        $user->removeVersionableGroup($group);
+        $this->assertTrue($user->isVersioningNecessary());
+        $this->assertGreaterThan(1, $user->save());
+        $this->assertEquals(3, $user->getVersion());
+        $this->assertEmpty($user->getOneVersion(3)->getVersionableGroupIds());
+    }
 }
Owner

willdurand commented Jun 7, 2012

The issue here is that you can't easily track changes in related collections… That means you always have to update a field, or to override isVersioningNecessary() to always return true.

Member

havvg commented Jun 7, 2012

Yeah, I stumbled upon this problem, too. That's why I added the enforceVersioning. It's a little helper and work around to get the job done for now.

I think at least related collections that are modified through the methods of the object should be recognized and create a new version. But yeah, it's tricky to get it working all the way :)

Owner

willdurand commented Jun 11, 2012

We really should find a way to detect changes in related collections..

Member

havvg commented Jun 11, 2012

What about a PropelRelatedObjectCollection, which adds a related object to a standard PropelObjectCollection.

The related object would be an observer on the collection and will retrieve updates on that collection to act on the changes. In this case: trigger a new version on next save().
This would introduce a contract between the collections that are actually bound to a related object. It would also distinguish those related collection from basic collections such as query results.

Owner

willdurand commented Jun 11, 2012

mmh sounds good

This pull request fails (merged 1364f9c7 into 66f9eda).

This pull request fails (merged 7a21725f into 66f9eda).

This pull request fails (merged 88612ea3 into 66f9eda).

Owner

willdurand commented Jul 24, 2012

Expected result: failed

This pull request fails (merged ba385f6 into dc5eeea).

@willdurand willdurand closed this Dec 28, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment