SF3中的新功能,我使用API平台和Sonata Media Bundle。
我在使用API平台GET请求获取Sonata的Gallery实体时被阻止。
"A circular reference has been detected when serializing the object of class \"Application\\Sonata\\MediaBundle\\Entity\\Gallery\" (configured limit: 1)"
实体的管理员工作得很好,我可以为实体添加一个库。 当实体有一个库时它会导致这个错误,当它没有时就可以了。
实体技术 API平台中的GET /技术
[
{
"id": 0,
"type": "string",
"comment": "string",
"links": [
"string"
],
"gallery": "string"
}
]
实体类
<?php
// src/AppBundle/Entity/Technic.php
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity
* @ApiResource
*/
class Technic
{
/**
* @var int The id of this evaluation.
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var string $type TechnicType of the evaluation
*
* @ORM\OneToOne(targetEntity="TechnicType")
* @Assert\NotBlank
*/
public $type;
/**
* @var string $note Note of the evaluation
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $comment;
/**
* @var Link[] Link Links of this technic.
*
* @ORM\ManyToMany(targetEntity="Link", cascade={"persist"})
*/
private $links;
/**
* @ORM\OneToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Gallery",cascade={"persist"})
* @ORM\JoinColumn(name="gallery", referencedColumnName="id", nullable=true)
*/
private $gallery;
/**
* Constructor
*/
public function __construct()
{
$this->links = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set type
*
* @param \AppBundle\Entity\TechnicType $type
*
* @return Technic
*/
public function setType(\AppBundle\Entity\TechnicType $type = null)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return \AppBundle\Entity\TechnicType
*/
public function getType()
{
return $this->type;
}
/**
* Add link
*
* @param \AppBundle\Entity\Link $link
*
* @return Technic
*/
public function addLink(\AppBundle\Entity\Link $link)
{
$this->links[] = $link;
return $this;
}
/**
* Remove link
*
* @param \AppBundle\Entity\Link $link
*/
public function removeLink(\AppBundle\Entity\Link $link)
{
$this->links->removeElement($link);
}
/**
* Get links
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getLinks()
{
return $this->links;
}
/**
* Set comment
*
* @param string $comment
*
* @return Technic
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* @return string
*/
public function getComment()
{
return $this->comment;
}
/**
* Set gallery
*
* @param \Application\Sonata\MediaBundle\Entity\Gallery $gallery
*
* @return Technic
*/
public function setGallery(\Application\Sonata\MediaBundle\Entity\Gallery $gallery = null)
{
$this->gallery = $gallery;
return $this;
}
/**
* Get gallery
*
* @return \Application\Sonata\MediaBundle\Entity\Gallery
*/
public function getGallery()
{
return $this->gallery;
}
}
感谢很多人,我很擅长我在StackQ / A,注释,seraliazer配置中尝试了很多东西......
答案 0 :(得分:1)
您需要正确配置序列化。设置序列化组,以便在GETting上一些实体序列化器只选择(例如)相关实体的ID,或者在规范化器中设置circualr引用处理程序并将该规范化器注入序列化器。
$normalizer = new GetSetMethodNormalizer();
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
api-platform可能有更具体的答案,我不知道,因为相关实体的序列化是很受欢迎的问题。