14

Hello in my project Symfony i want to convert my Entity to array Json so i choose the serializer for make this , and also i want to ignored some attributes because the convert is too long, so i read the documentation Symfony http://symfony.com/doc/current/components/serializer.html#ignoring-attributes and i copy this code

 $flowsites =    $this->getDoctrine()->getRepository("QSCORBundle:Flow_Site")->findAll();
 $normalizer = new ObjectNormalizer();
 $normalizer->setIgnoredAttributes(array('company')); 
 $encoder = new JsonEncoder();

 $serializer = new Serializer(array($normalizer), array($encoder));
 $flow_sites = $serializer->serialize($flow_sites, 'json');

 var_dump( $flow_sites );
 die();

after this the error generate A circular reference has been detected (configured limit: 1) so for resolve this problem i make some change to my code

$normalizer = new ObjectNormalizer(null);
$normalizer->setIgnoredAttributes(array('company', 'origin'));
$normalizer->setCircularReferenceHandler(function ($object) {
    return $object->getId();
});
$encoder = new JsonEncoder();
//$serializer = $this->get('serializer');
$serializer = new Serializer(array($normalizer), array($encoder));

$flowsites = $this->getDoctrine()->getRepository("QSCORBundle:Flow_Site")->findAll();
$jsonflowsites =  $serializer->serialize( $flowsites, 'json');
var_dump( $jsonflowsites );
die();
Mourad MAMASSI
  • 849
  • 1
  • 11
  • 14
  • 4
    add as an answer to your question, and vote for it ! – Vladimir Ch Nov 03 '16 at 08:20
  • your question is dublicated for this question: http://stackoverflow.com/questions/10730949/doctrine-entity-to-json-using-getsetmethodnormalizer-return-fatal-error/12027649#12027649 same answer solite your problem – Ahmet Erkan ÇELİK Feb 20 '17 at 14:12

0 Answers0