【问题标题】:API Platform Sonata Media Bundle Gallery - Circular ReferenceAPI 平台 Sonata Media Bundle Gallery - 循环参考
【发布时间】:2017-12-29 03:21:38
【问题描述】:

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

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

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

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

实体技术 在 API 平台中获取 /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 配置中尝试了很多东西......

【问题讨论】:

  • 这绝对看起来像没有完全配置的序列化程序配置。你试过什么?
  • 嗨,你的时间。在安装奏鸣曲管理和媒体包时,我遵循了所有安装和配置文档。 config.xml 和其他文件被填写为文档提及。也许它在有问题的捆绑包中有问题。

标签: symfony doctrine-orm sonata sonata-media-bundle api-platform.com


【解决方案1】:

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

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

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

【讨论】:

  • 使用捆绑包,它们自己使用特定的序列化程序资产,我认为我不必自己管理它,但我会深入研究配置。肯定问题来自此类问题,但我只是不明白为什么它不由捆绑包管理。正如您提到的,这可能只是 API 平台不兼容。我还将使用序列化程序组进行挖掘,以仅获取避免循环的部分数据,但这种类型的捆绑结构很复杂。我会尽快验证并完成您的回答。
  • 我没有提到任何不兼容性。顺便说一句,在管理对实体的某些属性的访问时,您可能还可以很好地使用序列化组。
猜你喜欢
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 2018-01-16
  • 2016-09-28
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多