1

SF3 中的新功能,我使用 API 平台和 Sonata Media Bundle。

我在使用 API 平台 GET 请求获取奏鸣曲的画廊实体时被阻止。

"A circular reference has been detected when serializing the object of class \"Application\\Sonata\\MediaBundle\\Entity\\Gallery\" (configured limit: 1)"

实体的管理员工作得很好,我可以为实体添加一个画廊。当实体有画廊时,它会导致此错误,当它没有时,它没关系。

API 平台中的实体技术 GET /technics

[
  {
    "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 配置中尝试了很多东西......

4

1 回答 1

1

您需要正确配置序列化。设置序列化组,以便在获取某些实体序列化程序时只会选择(例如)相关实体的 ID,或者在规范化程序中设置循环引用处理程序并将此规范化程序注入序列化程序。

$normalizer = new GetSetMethodNormalizer();
$normalizer->setCircularReferenceHandler(function ($object) {
    return $object->getId();
});

api-platform 可能有更具体的答案,我不知道,因为相关实体的序列化是流行的问题。

于 2017-07-24T08:50:24.197 回答