我有一个简单的实体类:
<?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;
}
}
当我尝试获取所有用户并在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);
}
我得到已检测到循环引用(配置限制:1)。 当我删除getScores getter时,一切正常。我只需要获取id,firstName和lastName。有没有办法不序列化其他对象?
答案 0 :(得分:5)
当序列化具有关系的实体时,使用circular references处理是很常见的事情。
解决方案没有。 1:实现可序列化接口并使关系属性不是序列化/反序列化(大多数情况下,它是一个有效的解决方案)
解决方案没有。 2:此规范化程序的setCircularReferenceLimit()
方法设置在将其视为循环引用之前序列化同一对象的次数。它的默认值是1.因此,在调用serialize()
方法之前,请执行以下操作:
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);
}
****更新****
正如@Derek在评论中所说,解决方案没有。在某些版本的Symfony中,2可能无效。然后,您可以尝试以这种方式为循环引用设置处理程序:
$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'));
这应返回您的实体值,而不是迭代关系。