Skip to content

Commit 1700492

Browse files
committed
Deprecate overring the __sleep method of ClassMetadata or PersistentCollection
1 parent bf2e243 commit 1700492

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/Mapping/ClassMetadata.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use ProxyManager\Proxy\GhostObjectInterface;
3535
use ReflectionClass;
3636
use ReflectionEnum;
37+
use ReflectionMethod;
3738
use ReflectionNamedType;
3839
use ReflectionProperty;
3940
use Symfony\Component\Uid\UuidV1;
@@ -2640,10 +2641,32 @@ public function mapField(array $mapping): array
26402641
* That means any metadata properties that are not set or empty or simply have
26412642
* their default value are NOT serialized.
26422643
*
2643-
* Parts that are also NOT serialized because they cannot be properly unserialized:
2644-
* - reflClass (ReflectionClass)
2645-
* - reflFields (ReflectionProperty array)
2646-
* - propertyAccessors (ReflectionProperty array)
2644+
* Parts that are also NOT serialized because they cannot be properly unserialized:
2645+
* - reflClass (ReflectionClass)
2646+
* - reflFields (ReflectionProperty array)
2647+
* - propertyAccessors (ReflectionProperty array)
2648+
*/
2649+
public function __serialize(): array
2650+
{
2651+
if (new ReflectionMethod($this, '__sleep')->getDeclaringClass() !== self::class) {
2652+
trigger_deprecation(
2653+
'doctrine/mongodb-odm',
2654+
'2.16',
2655+
'The method __sleep() is deprecated. Implement and use %s() instead.',
2656+
__METHOD__,
2657+
);
2658+
}
2659+
2660+
$data = [];
2661+
foreach ($this->__sleep() as $field) {
2662+
$data[$field] = $this->$field;
2663+
}
2664+
2665+
return $data;
2666+
}
2667+
2668+
/**
2669+
* @deprecated Implement and use __serialize() instead.
26472670
*
26482671
* @return array The names of all the fields that should be serialized.
26492672
*/
@@ -2700,7 +2723,7 @@ public function __sleep()
27002723
$serialized[] = 'isQueryResultDocument';
27012724
}
27022725

2703-
if ($this->isView()) {
2726+
if ($this->isView) {
27042727
$serialized[] = 'isView';
27052728
$serialized[] = 'rootClass';
27062729
}
@@ -2745,8 +2768,18 @@ public function __sleep()
27452768
}
27462769

27472770
/**
2748-
* Restores some state that cannot be serialized/unserialized.
2771+
* Restores the serialized values and some state that cannot be serialized/unserialized.
27492772
*/
2773+
public function __unserialize(array $data): void
2774+
{
2775+
foreach ($data as $field => $value) {
2776+
$this->$field = $value;
2777+
}
2778+
2779+
$this->__wakeup();
2780+
}
2781+
2782+
/** @deprecated Implement and use __unserialize() instead. */
27502783
public function __wakeup(): void
27512784
{
27522785
// Restore ReflectionClass and properties

src/PersistentCollection/PersistentCollectionTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@ public function slice($offset, $length = null)
509509
* Called by PHP when this collection is serialized. Ensures that the
510510
* internal state of the collection can be reproduced after serialization
511511
*
512+
* @return array<string, mixed>
513+
*/
514+
public function __serialize(): array
515+
{
516+
return [
517+
'coll' => $this->coll,
518+
'initialized' => $this->initialized,
519+
'mongoData' => $this->mongoData,
520+
'snapshot' => $this->snapshot,
521+
'isDirty' => $this->isDirty,
522+
'hints' => $this->hints,
523+
];
524+
}
525+
526+
/**
527+
* @deprecated Implement and use __serialize() instead.
528+
*
512529
* @return string[]
513530
*/
514531
public function __sleep()

0 commit comments

Comments
 (0)