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.
- Converting a Symfony2 PHP entity object for use within Javascript
- A circular reference has been detected (configured limit: 1) Serializer
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);
$query->setParameter(1,$user);
$employees = $query->getResult();
I Tryied these thing:
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');
The sencond proof
In these proof I readed by default on Symfony 3 Serialzer is deseabe. For that I modified these files:
app/config/config.yml
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
serializer:
enabled: true
enable_annotations: true
The unic important thing on here are the last lines where I active the serializer.
app/config/servces.yml
services:
get_set_method_normalizer:
class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
public: false
tags:
- { name: serializer.normalizer }
src/AppBundle/Controller/DefaultController.php
<?php
$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).