Skip to content

Commit bd85865

Browse files
committed
feature #4299 [Serializer] Handle circular references. symfony/symfony#12098. (dunglas)
This PR was merged into the master branch. Discussion ---------- [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 Commits ------- 1a1476e [Serializer] Handle circular references. symfony/symfony#12098.
2 parents a58b0d1 + 1a1476e commit bd85865

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

Diff for: components/serializer.rst

+96-1
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,109 @@ When serializing, you can set a callback to format a specific object property::
238238
$serializer->serialize($person, 'json');
239239
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
240240

241+
Handling Circular References
242+
----------------------------
243+
244+
.. versionadded:: 2.6
245+
Handling of circular references was introduced in Symfony 2.6. In previous
246+
versions of Symfony, circular references led to infinite loops.
247+
248+
Circular references are common when dealing with entity relations::
249+
250+
class Organization
251+
{
252+
private $name;
253+
private $members;
254+
255+
public function setName($name)
256+
{
257+
$this->name = $name;
258+
}
259+
260+
public function getName()
261+
{
262+
return $this->name;
263+
}
264+
265+
public function setMembers(array $members)
266+
{
267+
$this->members = $members;
268+
}
269+
270+
public function getMembers()
271+
{
272+
return $this->members;
273+
}
274+
}
275+
276+
class Member
277+
{
278+
private $name;
279+
private $organization;
280+
281+
public function setName($name)
282+
{
283+
$this->name = $name;
284+
}
285+
286+
public function getName()
287+
{
288+
return $this->name;
289+
}
290+
291+
public function setOrganization(Organization $organization)
292+
{
293+
$this->organization = $organization;
294+
}
295+
296+
public function getOrganization()
297+
{
298+
return $this->organization;
299+
}
300+
}
301+
302+
To avoid infinite loops, :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer`
303+
throws a :class:`Symfony\\Component\\Serializer\\Exception\\CircularReferenceException`
304+
when such case is encountered::
305+
306+
$member = new Member();
307+
$member->setName('Kévin');
308+
309+
$org = new Organization();
310+
$org->setName('Les-Tilleuls.coop');
311+
$org->setMembers(array($member));
312+
313+
$member->setOrganization($kevin);
314+
315+
echo $serializer->serialize($org, 'json'); // Throws a CircularReferenceException
316+
317+
The ``setCircularReferenceLimit()`` method of this normalizer sets the number
318+
of times serializing the same object must occur before considering being
319+
in a circular reference. Its default value is ``1``.
320+
321+
Instead of throwing an exception, circular references can also be handled
322+
by custom callables. This is especially useful when serializing entities
323+
having unique identifiers::
324+
325+
$encoder = new JsonEncoder();
326+
$normalizer = new GetSetMethodNormalizer();
327+
328+
$normalizer->setCircularReferenceHandler(function ($object) {
329+
return $object->getName();
330+
});
331+
332+
$serializer = new Serializer(array($normalizer), array($encoder));
333+
echo $serializer->serialize($org, 'json');
334+
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"]}
335+
241336
JMSSerializer
242337
-------------
243338

244339
A popular third-party library, `JMS serializer`_, provides a more
245340
sophisticated albeit more complex solution. This library includes the
246341
ability to configure how your objects should be serialized/deserialized via
247342
annotations (as well as YAML, XML and PHP), integration with the Doctrine ORM,
248-
and handling of other complex cases (e.g. circular references).
343+
and handling of other complex cases.
249344

250345
.. _`JMS serializer`: https://github.com./schmittjoh/serializer
251346
.. _Packagist: https://packagist.org/packages/symfony/serializer

0 commit comments

Comments
 (0)