Skip to content
Browse files

Fix bug #478

Avoid clearing references on BaseObject in __sleep method
because this breaks setting foreign references. By cloning the
BaseObject itself we ensure the the Baseobject itself is not
changing when calculating the has code
Added Unit-Tests for the bug
  • Loading branch information...
1 parent 4f33db4 commit 45cce09419932c7d451db9e29a82371b4bf1a258 @danez danez committed May 3, 2013
Showing with 40 additions and 1 deletion.
  1. +3 −1 runtime/lib/om/BaseObject.php
  2. +37 −0 test/testsuite/generator/builder/om/GeneratedObjectRelTest.php
View
4 runtime/lib/om/BaseObject.php
@@ -292,7 +292,9 @@ public function hashCode()
return crc32(serialize($this->getPrimaryKey()));
}
- return crc32(serialize($this));
+ $clone = clone $this;
+ $clone->clearAllReferences();
+ return crc32(serialize($clone));
}
/**
View
37 test/testsuite/generator/builder/om/GeneratedObjectRelTest.php
@@ -178,6 +178,43 @@ public function testOneToManyGetter()
$this->assertNotNull($books->getCurrent(), 'getRelCol() initialize the internal iterator at the beginning');
}
+ public function testOneToManySetterIsNotLoosingRelation()
+ {
+ $author = new Author();
+ $author->setFirstName('John');
+ $author->setLastName('Doe');
+ $book = new Book();
+ $book->setTitle('Foo4');
+ $book->setISBN('1234');
+
+ $book->setAuthor($author);
+
+ $this->assertNotNull($book->getAuthor(), 'setRel is clearing reference on object');
+ }
+
+ public function testOneToManySetterColelctionIsNotLoosingRelation()
+ {
+ $author = new Author();
+ $author->setFirstName('John');
+ $author->setLastName('Doe');
+ $book1 = new Book();
+ $book1->setTitle('Foo4');
+ $book1->setISBN('1234');
+ $book2 = new Book();
+ $book2->setTitle('Foo6');
+ $book2->setISBN('1239');
+
+ $author->addBook($book1);
+ $author->addBook($book2);
+
+ $books = $author->getBooks();
+ $this->assertCount(2, $books, 'setRelCol is losing references to object');
+
+ foreach ($books as $book) {
+ $this->assertNotNull($book->getAuthor(), 'setRelCol is losing backreference on set object');
+ }
+ }
+
public function testManyToManyCounterExists()
{
$this->assertTrue(method_exists('BookClubList', 'countBooks'), 'Object generator correcly adds counter for the crossRefFk');

0 comments on commit 45cce09

Please sign in to comment.
Something went wrong with that request. Please try again.