File: serialization.rst

package info (click to toggle)
php-doctrine-collections 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 468 kB
  • sloc: php: 2,531; makefile: 18
file content (29 lines) | stat: -rw-r--r-- 886 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Serialization
=============

Using (un-)serialize() on a collection is not a supported use-case
and may break when changes on the collection's internals happen in the future.
If a collection needs to be serialized, use ``toArray()`` and reconstruct
the collection manually.

.. code-block:: php

    $collection = new ArrayCollection([1, 2, 3]);
    $serialized = serialize($collection->toArray());

A reconstruction is also necessary when the collection contains objects with
infinite recursion of dependencies like in this ``json_serialize()`` example:

.. code-block:: php

    $foo = new Foo();
    $bar = new Bar();

    $foo->setBar($bar);
    $bar->setFoo($foo);

    $collection = new ArrayCollection([$foo]);
    $json       = json_serialize($collection->toArray()); // recursion detected

Serializer libraries can be used to create the serialization-output to prevent
errors.