Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Serialize doctrine entity

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?

like image 577
snaksa Avatar asked Jun 28 '16 07:06

snaksa


1 Answers

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.

like image 70
Muriano Avatar answered Oct 30 '22 22:10

Muriano