1
votes

New in SF3, I use API Platform and Sonata Media Bundle.

I'm blocked while getting Gallery entity of Sonata using API Platform GET request.

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

The admin of the entity works great, I can add a gallery to the entity. When the entity have a gallery it cause this error, when it does not it's ok.

Entity Technic GET /technics in API Platform

[
  {
    "id": 0,
    "type": "string",
    "comment": "string",
    "links": [
      "string"
    ],
    "gallery": "string"
  }
]

Entity Class

<?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;
    }
}

Thank a lot guys, I'm desesperate I try a lot of things in StackQ/A, annotations, seraliazer config...

1
It definitely seems like not fully configured serializer configuration. What have you tried? - svgrafov
Hi, ty for your time. I followed all install and config documentation when installing sonata admin and media bundle. config.xml and others are filled up as the documentation mention. Maybe it have a problem in the bundle in question. - Paul Leclerc

1 Answers

1
votes

You need to configure serialization correctly. Either setup serialization groups, so that on GETting some entity serializer would only pick (for example) IDs of related entities, or set up circualr reference handler in normalizer and inject this normalizer into serializer.

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

There might be more specific answer for api-platform, which I don't know, because serialization of related entities is popular issue.