Skip to content

Commit 4de0a04

Browse files
authored
Merge pull request #1929 from alcaeus/fix-persistent-collection-serialisation
Fix serialisation of uninitialised PersistentCollection instances
2 parents 69c0064 + 4097963 commit 4de0a04

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,15 @@ public function slice($offset, $length = null)
574574
}
575575

576576
/**
577-
* Called by PHP when this collection is serialized. Ensures that only the
578-
* elements are properly serialized.
577+
* Called by PHP when this collection is serialized. Ensures that the
578+
* internal state of the collection can be reproduced after serialization
579579
*
580580
* @internal Tried to implement Serializable first but that did not work well
581581
* with circular references. This solution seems simpler and works well.
582582
*/
583583
public function __sleep()
584584
{
585-
return array('coll', 'initialized');
585+
return array('coll', 'initialized', 'mongoData', 'snapshot', 'isDirty', 'hints');
586586
}
587587

588588
/* ArrayAccess implementation */

tests/Doctrine/ODM/MongoDB/Tests/PersistentCollectionTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Doctrine\Common\Collections\ArrayCollection;
66
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
77
use Doctrine\ODM\MongoDB\PersistentCollection;
8+
use Documents\User;
9+
use MongoId;
10+
use stdClass;
811

912
class PersistentCollectionTest extends BaseTest
1013
{
@@ -68,6 +71,54 @@ public function testGetTypeClassWorksAfterUnserialization()
6871
$this->assertInstanceOf(ClassMetadata::class, $unserialized->getTypeClass());
6972
}
7073

74+
public function testMongoDataIsPreservedDuringSerialization()
75+
{
76+
$mongoData = [
77+
[
78+
'$ref' => 'group',
79+
'$id' => new MongoId()
80+
],
81+
[
82+
'$ref' => 'group',
83+
'$id' => new MongoId()
84+
],
85+
];
86+
87+
$collection = new PersistentCollection(new ArrayCollection(), $this->dm, $this->uow);
88+
$collection->setMongoData($mongoData);
89+
90+
$serialized = serialize($collection);
91+
/** @var PersistentCollection $unserialized */
92+
$unserialized = unserialize($serialized);
93+
94+
$unserialized->setOwner(new \Documents\User(), $this->dm->getClassMetadata(User::class)->getFieldMapping('groups'));
95+
$unserialized->setDocumentManager($this->dm);
96+
97+
$this->assertCount(2, $unserialized->getMongoData());
98+
}
99+
100+
public function testSnapshotIsPreservedDuringSerialization()
101+
{
102+
$collection = new PersistentCollection(new ArrayCollection(), $this->dm, $this->uow);
103+
$collection->add(new stdClass());
104+
$collection->takeSnapshot();
105+
106+
$this->assertCount(1, $collection->getSnapshot());
107+
$this->assertFalse($collection->isDirty());
108+
$this->assertCount(1, $collection->unwrap());
109+
110+
$serialized = serialize($collection);
111+
/** @var PersistentCollection $unserialized */
112+
$unserialized = unserialize($serialized);
113+
114+
$unserialized->setOwner(new \Documents\User(), $this->dm->getClassMetadata(User::class)->getFieldMapping('groups'));
115+
$unserialized->setDocumentManager($this->dm);
116+
117+
$this->assertCount(1, $unserialized->getSnapshot());
118+
$this->assertFalse($unserialized->isDirty());
119+
$this->assertCount(1, $unserialized->unwrap());
120+
}
121+
71122
/**
72123
* @param array $expected
73124
* @param array $snapshot

0 commit comments

Comments
 (0)