Symfony и Api-Platform: проблема в сообщении и размещение с отношением ManyToMany - PullRequest
0 голосов
/ 26 мая 2020

У меня есть 2 объекта Foo & Bar с отношением ManyToMany:

    namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ApiResource(
 *  normalizationContext={"groups"={"foo:read"}},
 *  denormalizationContext={"groups"={"foo:write"}},
 *   collectionOperations={
 *     "get",
 *     "post"={"security"="is_granted('ROLE_DEVROOT')"}
 *   },
 *   itemOperations={
 *      "get",
 *      "put"={"security"="is_granted('ROLE_DEVROOT')"},
 *      "delete"={"security"="is_granted('ROLE_DEVROOT')"},
 *   }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\FooRepository")
 * @Gedmo\SoftDeleteable()
 * @UniqueEntity(fields={"label"}, message="There is already a PTUR with this label")
 */
//Direct access from API (No DTO)
class Foo
{
    use TimestampableEntity;
    use SoftDeleteableEntity;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"foo:read", "foo:write"})
     */
    private $id;
    ...

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Bar", inversedBy="foos", cascade="all")
     * @ORM\JoinTable(
     *     name="footobar",
     *     joinColumns={@ORM\JoinColumn(name="foo_id", referencedColumnName="id", nullable=false)},
     *     inverseJoinColumns={@ORM\JoinColumn(name="bar_id", referencedColumnName="id", nullable=false)}
     * )
     * @Groups({"foo:read", "foo:write"})
     */
    private $bars;

    ...
    public function __construct()
    {
        $this->bars = new ArrayCollection();
    }

    ...

    /**
     * @return Collection|Bar[]
     * @Groups({"foo:read", "foo:write"})
     */
    public function getBars(): Collection
    {
        return $this->bars;
    }

    public function addBars(Bar $bars): self
    {
        if (!$this->bars->contains($bars)) {
            $this->bars[] = $bars;
        }

        return $this;
    }

    public function removeBars(Bar $bars): self
    {
        if ($this->bars->contains($bars)) {
            $this->bars->removeElement($bars);
        }

        return $this;
    }

    ...

    public function __toString(){
        return $this->label;
    }

}




namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ApiResource(
 *  normalizationContext={"groups"={"bar:read"}},
 *  denormalizationContext={"groups"={"bar:write"}},
 *   collectionOperations={
 *     "get",
 *     "post"={"security"="is_granted('ROLE_DEVROOT')"}
 *   },
 *   itemOperations={
 *      "get",
 *      "put"={"security"="is_granted('ROLE_DEVROOT')"},
 *      "delete"={"security"="is_granted('ROLE_DEVROOT')"},
 *   }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\BarRepository")
 * @Gedmo\SoftDeleteable()
 * @UniqueEntity(fields={"label"}, message="There is already a PTU with this label")
 */
//Direct access from API (No DTO)
class Bar
{
    use TimestampableEntity;
    use SoftDeleteableEntity;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"bar:read", "bar:write"})
     */
    private $id;

    ...

    /**
     * @ORM\Column(type="boolean")
     * @Groups({"bar:read", "bar:write"})
     */
    private $isdefault = false;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Foo", mappedBy="bars")
     * @Groups({"bar:read", "bar:write"})
     */
    private $foos;

    public function __construct()
    {
        $this->foos = new ArrayCollection();
    }

    ...

    public function __toString(){
        return $this->label;
    }

    /**
     * @return Collection|Foo[]
     * @Groups({"bar:read", "bar:write"})
     */
    public function getFoos(): Collection
    {
        return $this->foos;
    }

    public function addFoo(Foo $foo): self
    {
        if (!$this->foos->contains($foo)) {
            $this->foos[] = $foo;
            $foo->addBars($this);
        }

        return $this;
    }

    public function removeFoo(Foo $foo): self
    {
        if ($this->foos->contains($foo)) {
            $this->foos->removeElement($foo);
            $foo->removeBars($this);
        }

        return $this;
    }
}

Когда я делаю запрос POST или PUT на Foo, кросс-таблица не заполняется (проверяется с помощью phpmyadmin) (JSON) :

{"id": 3, "label": "ccccc", "bars": ["/ xxx / api / bars / 1", "/ xxx / api / bars / 3" ], "enrangebase": true, "enabled": true, "isdefault": false}

Что я пропустил? Есть ли способ отследить ошибку?

Спасибо за вашу помощь,

EDIT1: добавлены группы нормализации EDIT2: добавлены группы нормализации в Bar

...