| 1 |
Serialization |
| 2 |
============= |
| 3 |
|
| 4 |
Using (un-)serialize() on a collection is not a supported use-case |
| 5 |
and may break when changes on the collection's internals happen in the future. |
| 6 |
If a collection needs to be serialized, use ``toArray()`` and reconstruct |
| 7 |
the collection manually. |
| 8 |
|
| 9 |
.. code-block:: php |
| 10 |
|
| 11 |
$collection = new ArrayCollection([1, 2, 3]); |
| 12 |
$serialized = serialize($collection->toArray()); |
| 13 |
|
| 14 |
A reconstruction is also necessary when the collection contains objects with |
| 15 |
infinite recursion of dependencies like in this ``json_serialize()`` example: |
| 16 |
|
| 17 |
.. code-block:: php |
| 18 |
|
| 19 |
$foo = new Foo(); |
| 20 |
$bar = new Bar(); |
| 21 |
|
| 22 |
$foo->setBar($bar); |
| 23 |
$bar->setFoo($foo); |
| 24 |
|
| 25 |
$collection = new ArrayCollection([$foo]); |
| 26 |
$json = json_serialize($collection->toArray()); // recursion detected |
| 27 |
|
| 28 |
Serializer libraries can be used to create the serialization-output to prevent |
| 29 |
errors. |