Permalink
Browse files

Fix: some isVersionningNecessary() calls can cause fatal errors

Given the following model, some isVersionningNecessary() calls would cause
fatal errors.

    User                              Group
    ----                              -----
    name                              name
    group_id [FK, not required]

When saving an user with no group but a name, the isVersioningNecessary()
method returns true because the object is modified. But if we save again the
same user - as there are no modifications on the user - the foreign keys
are checked and isVersioningNecessary() is called on them, which causes
a fatal error when one of them is not set.
  • Loading branch information...
1 parent 356947a commit eb303e52f92810c446ea82a99622ed0ee63b9ed9 @K-Phoen K-Phoen committed Mar 14, 2012
@@ -184,7 +184,7 @@ public function isVersioningNecessary(\$con = null)
foreach ($this->behavior->getVersionableFks() as $fk) {
$fkGetter = $this->builder->getFKPhpNameAffix($fk, $plural = false);
$script .= "
- if (\$this->get{$fkGetter}(\$con)->isVersioningNecessary(\$con)) {
+ if (null !== (\$object = \$this->get{$fkGetter}(\$con)) && \$object->isVersioningNecessary(\$con)) {
return true;
}
";
@@ -505,6 +505,19 @@ public function testIsVersioningNecessary()
$this->assertFalse($a->isVersioningNecessary());
}
+ public function testIsVersioningNecessaryWithNullFk()
+ {
+ // the purpose of this tests is to highlight a bug with FK and isVersioningNecessary()
+ $b1 = new VersionableBehaviorTest5();
+ $b1->setNew(false);
+
+ // this time, the object isn't modified, so the isVersioningNecessary()
+ // method is called on FK objects... which can be null.
+ $b1->isVersioningNecessary();
+
+ $this->assertTrue(true, 'getting here means that nothing went wrong');
+ }
+
public function testAddVersionNewObject()
{
VersionableBehaviorTest1Peer::disableVersioning();

0 comments on commit eb303e5

Please sign in to comment.