Answers for "A circular reference has been detected when serializing the object of class"

PHP
0

A circular reference has been detected when serializing the object of class

use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer; 

$encoder = new JsonEncoder();
        $defaultContext = [
            AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
                return $object->getId();
            },
        ];
        $normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
        $serializer = new Serializer([$normalizer], [$encoder]);
        return $serializer->normalize($yourObj, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]);
Posted by: Guest on September-28-2021

Code answers related to "A circular reference has been detected when serializing the object of class"

Browse Popular Code Answers by Language