6

I have a simple entity class:

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * @ORM\Entity(repositoryClass="CompanyUserRepository")
 * @ORM\Table(name="company_users")
 */
class CompanyUser
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=100)
 */
private $firstName;

/**
 * @ORM\Column(type="string", length=100)
 */
private $lastName ;

/**
 * @ORM\OneToMany(targetEntity="Score", mappedBy="user")
 */
private $scores;
/**
 * Constructor
 */
public function __construct()
{
    $this->scores = new ArrayCollection();
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set firstName
 *
 * @param string $firstName
 *
 * @return CompanyUser
 */
public function setFirstName($firstName)
{
    $this->firstName = $firstName;

    return $this;
}

/**
 * Get firstName
 *
 * @return string
 */
public function getFirstName()
{
    return $this->firstName;
}

/**
 * Set lastName
 *
 * @param string $lastName
 *
 * @return CompanyUser
 */
public function setLastName($lastName)
{
    $this->lastName = $lastName;

    return $this;
}

/**
 * Get lastName
 *
 * @return string
 */
public function getLastName()
{
    return $this->lastName;
}

/**
 * Add score
 *
 * @param \AppBundle\Entity\Score $score
 *
 * @return CompanyUser
 */
public function addScore(\AppBundle\Entity\Score $score)
{
    $this->scores[] = $score;

    return $this;
}

/**
 * Remove score
 *
 * @param \AppBundle\Entity\Score $score
 */
public function removeScore(\AppBundle\Entity\Score $score)
{
    $this->scores->removeElement($score);
}

/**
 * Get scores
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getScores()
{
    return $this->scores;
}

}

The problem comes when I try to get all users and serialize them in json:

/**
 * @Route("/api/getUsers", name="getUsers")
 */
public function getUsers(Request $request){
    $users = $this->getDoctrine()
    ->getRepository('AppBundle:CompanyUser')
    ->findAll();

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

    return new Response($users);
}

I get A circular reference has been detected (configured limit: 1). When I remove the getScores getter everything works fine. I need to get only the id, firstName and lastName. Is there a way to not serialize the other objects?

1
  • 1
    It is a good practice to implement \Serializable interface to objects that might be serialized. Take account of this.
    – Muriano
    Jun 28, 2016 at 8:24

1 Answer 1

9

Well, it is a common thing to handle with circular references when serializing entities with relations.

Solution no. 1: Implements serializable interface and make relations attributes are not serialize/unserialize (most cases, it is a valid solution)

Solution no. 2: The setCircularReferenceLimit() method of this normalizer sets the number of times it will serialize the same object before considering it a circular reference. Its default value is 1. So, before calling serialize() method, do this:

public function getUsers(Request $request){
    $users = $this->getDoctrine()
    ->getRepository('AppBundle:CompanyUser')
    ->findAll();

    $serializer = $this->get('serializer');
    $serializer->setCircularReferenceLimit(2); // Change this with a proper value for your case
    $data = $serializer->serialize($users, 'json');

    return new Response($data);
}

**** UPDATE ****

As @Derek says in his comment, solution no. 2 can be invalid in some versions of Symfony. Then you can try to set a handler for circular references this way:

$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();

$normalizer->setCircularReferenceHandler(function ($object) {
   return $object->getName(); // Change this to a valid method of your object
});

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

This should return your entity value instead to iterate over relations.

3
  • Your solution no. 2 does not work. The setCircularReferenceLimit() method does not belong the the Serializer class but to AbstractNormalizer. Your code (in Symfony 2.8 at least) throws a fatal error. I can't find a way to get this normalizer from the serializer service, as the getNormalizer() method is private...
    – Derek
    Jul 5, 2016 at 12:38
  • The getNormalizer() method is private, so it can't be used (and it requires 2 arguments). It seems that the normalizer can only be customized before injecting it into the serializer...
    – Derek
    Jul 6, 2016 at 7:13
  • sorry for the last comment @Derek , i was hurry. I've updated my response and I added a new way to handle this error.
    – Muriano
    Jul 6, 2016 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.