mohamet monte mohamet monte - 1 year ago 394
Javascript Question

Symfony 3 serializer problems

Hello I am creating an API REST on Symfony 3.1.

I have got a problem Serializing and object.

These is the error it returns to me.

A circular reference has been detected (configured limit: 1).

Stack overflow link I have read without any result.

These is the documentation which I readed for try the seialize my obtject.

Here is the code for fill $employees:

$em = $this->getDoctrine()->getManager();
$dql = " SELECT e FROM BackendBundle:Employees e
INNER JOIN BackendBundle:Companies c
WITH e.idCompany = c.idCompany
WHERE c.idUser = ?1";
$query = $em->createQuery($dql);
$employees = $query->getResult();

I Tryied these thing:

  1. First proof

    use Symfony\Component\Serializer\Serializer;
    use Symfony\Component\Serializer\Encoder\XmlEncoder;
    use Symfony\Component\Serializer\Encoder\JsonEncoder;

    use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
    $encoders = array(new XmlEncoder(), new JsonEncoder());
    $normalizers = array(new ObjectNormalizer());
    $serializer = new Serializer($normalizers,$encoders);
    $data = $serializer->serialize($employees, 'json');

  2. The sencond proof

In these proof I readed by default on Symfony 3 Serialzer is deseabe. For that I modified these files:


#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
enabled: true
enable_annotations: true

The unic important thing on here are the last lines where I active the serializer.


class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
public: false
- { name: serializer.normalizer }


$serializer = $this->get('serializer');
$json = $serializer->serialize($employees,'json');

And little more proof where there is not much difference between you are reading and it.

Please if someone know how serialize an object on Symfony 3. I wat trying it all morning with the same error.

A circular reference has been detected (configured limit: 1).

Answer Source

Your issue is that serializing Employees is serializing their company which again is having a reference to Employees, a perfect circular reference.

You can handle these circular references in Symfony's Serializer e.g. by catching the CircularReferenceException or by using custom callable in setCircularReferenceHandler and only serialize attributes which are not referencing back to the original entity.

See Symfony's documentation for a detailed description.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download