Skip to content

Commit 8866cff

Browse files
committed
update the serialization format of entities
The unserialize method is backward compatible and should still work with data serialized before the switch to the new format. You may want to extend the format to your needs in your childclasses `__serialize` and `__unserialize` methods. Don't forget to call the parent methods though.
1 parent 76dc54b commit 8866cff

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/Entity.php

+20-14
Original file line numberDiff line numberDiff line change
@@ -665,15 +665,6 @@ public function serialize()
665665
return serialize($this->__serialize());
666666
}
667667

668-
public function __serialize(): array
669-
{
670-
return [
671-
$this->data,
672-
$this->relatedObjects,
673-
$this->exists,
674-
];
675-
}
676-
677668
/**
678669
* Constructs the object
679670
*
@@ -682,15 +673,30 @@ public function __serialize(): array
682673
*/
683674
public function unserialize($serialized)
684675
{
685-
static::bootIfNotBooted();
686-
$data = unserialize($serialized);
687-
$this->__unserialize($data);
676+
$this->__unserialize(unserialize($serialized));
677+
}
678+
679+
public function __serialize()
680+
{
681+
return [
682+
'data' => $this->data,
683+
'related' => $this->relatedObjects,
684+
'exists' => $this->exists
685+
];
688686
}
689687

690-
public function __unserialize(array $data): void
688+
public function __unserialize($data)
691689
{
692690
static::bootIfNotBooted();
693-
list($this->data, $this->relatedObjects, $this->exists) = $data;
691+
// backward compatibility
692+
if (array_keys($data) === range(0, count($data) - 1)) {
693+
list($this->data, $this->relatedObjects, $this->exists) = $data;
694+
} else {
695+
$this->data = isset($data['data']) ? $data['data'] : [];
696+
$this->relatedObjects = isset($data['related']) ? $data['related'] : [];
697+
$this->exists = isset($data['exists']) ? $data['exists'] : false;
698+
}
699+
694700
$this->entityManager = EM::getInstance(static::class);
695701
$this->onInit(false);
696702
}

tests/Entity/DataTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,11 @@ public function serialization()
348348

349349
$serialized = $entity->serialize();
350350

351-
self::assertSame(serialize([['foo' => 'bar'], [], true]), $serialized);
351+
self::assertSame(serialize([
352+
'data' => ['foo' => 'bar'],
353+
'related' => [],
354+
'exists' => true
355+
]), $serialized);
352356
}
353357

354358
/** @test */
@@ -362,6 +366,18 @@ public function deserialization()
362366
self::assertSame('bar', $entity->foo);
363367
}
364368

369+
/** @test */
370+
public function deserializationWithOldFormat()
371+
{
372+
$serialized = 'C:35:"ORM\Test\Entity\Examples\StudlyCaps":54:' .
373+
'{a:3:{i:0;a:1:{s:3:"foo";s:3:"bar";}i:1;a:0:{}i:2;b:0;}}';
374+
375+
$entity = unserialize($serialized);
376+
377+
self::assertInstanceOf(StudlyCaps::class, $entity);
378+
self::assertSame('bar', $entity->foo);
379+
}
380+
365381
/** @test */
366382
public function unserializeCallsOnInit()
367383
{

0 commit comments

Comments
 (0)