Browse files

feature #4299 [Serializer] Handle circular references. symfony/symfon…

…y#12098. (dunglas)

This PR was merged into the master branch.


[Serializer] Handle circular references. symfony/symfony#12098.

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | symfony/symfony#12098
| Applies to    | 2.6 or 2.7
| Fixed tickets | n/a


1a1476e [Serializer] Handle circular references. symfony/symfony#12098.
  • Loading branch information...
weaverryan committed Oct 28, 2014
2 parents a58b0d1 + 1a1476e commit bd85865f4b5e64b72d00f6b4e29e9c2015cde01d
Showing with 96 additions and 1 deletion.
  1. +96 −1 components/serializer.rst
@@ -238,14 +238,109 @@ When serializing, you can set a callback to format a specific object property::
$serializer->serialize($person, 'json');
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
Handling Circular References
.. versionadded:: 2.6
Handling of circular references was introduced in Symfony 2.6. In previous
versions of Symfony, circular references led to infinite loops.
Circular references are common when dealing with entity relations::
class Organization
private $name;
private $members;
public function setName($name)
$this->name = $name;
public function getName()
return $this->name;
public function setMembers(array $members)
$this->members = $members;
public function getMembers()
return $this->members;
class Member
private $name;
private $organization;
public function setName($name)
$this->name = $name;
public function getName()
return $this->name;
public function setOrganization(Organization $organization)
$this->organization = $organization;
public function getOrganization()
return $this->organization;
To avoid infinite loops, :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer`
throws a :class:`Symfony\\Component\\Serializer\\Exception\\CircularReferenceException`
when such case is encountered::
$member = new Member();
$org = new Organization();
echo $serializer->serialize($org, 'json'); // Throws a CircularReferenceException
The ``setCircularReferenceLimit()`` method of this normalizer sets the number
of times serializing the same object must occur before considering being
in a circular reference. Its default value is ``1``.
Instead of throwing an exception, circular references can also be handled
by custom callables. This is especially useful when serializing entities
having unique identifiers::
$encoder = new JsonEncoder();
$normalizer = new GetSetMethodNormalizer();
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getName();
$serializer = new Serializer(array($normalizer), array($encoder));
echo $serializer->serialize($org, 'json');
// {"name":"","members":[{"name":"K\u00e9vin", organization: ""]}
A popular third-party library, `JMS serializer`_, provides a more
sophisticated albeit more complex solution. This library includes the
ability to configure how your objects should be serialized/deserialized via
annotations (as well as YAML, XML and PHP), integration with the Doctrine ORM,
and handling of other complex cases (e.g. circular references).
and handling of other complex cases.
.. _`JMS serializer`:
.. _Packagist:

0 comments on commit bd85865

Please sign in to comment.