Api-platform PUT 操作与 denormalization_context groups on itemOperations 不工作

Api-platform PUT operation with denormalization_context groups on an itemOperations not working

我有 2 个实体:

我只想检索 itemOperation 而不是 collectionOperations 的词,所以我添加了一个组 GET "item_words.read" 和 PUT denormalization_context.

它非常适合 GET 操作,出于某种原因 PUT 操作没有给我“单词”子资源。

获取:

放置:

这是我的代码:

项目

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use App\Controller\FileUploadController;
use Symfony\Component\Validator\Constraints as Assert;
use App\Controller\ItemVocabularyController;
use Symfony\Component\Serializer\Annotation\Groups;
use Gedmo\Mapping\Annotation as Gedmo;


/**
 * @ApiResource(
 *     normalizationContext={"groups"={"item.read"}},
 *     denormalizationContext={"groups"={"item.write"}},
 *     collectionOperations={"get", "post"},
 *     attributes={"order"={"words.value": "ASC"}},
 *     itemOperations={
 *          "get"={
 *              "normalization_context"={"groups"={"item.read","item_words.read"}},
 *          },
 *          "put"={
 *              "denormalization_context"={"groups"={"item.write","item_words.write"}},
 *          },
 *         "delete",
 *         "getItemVocabulary"={
 *                   "method"="GET",
 *                   "path"="/items/{id}/vocabulary",
 *                   "controller"=ItemVocabularyController::class,
 *                  }
 *     }
 * )
 * @ORM\Entity
 * @ORM\Table(name="`items`")
 */
class Item
{
    /**
     *
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank(message="Le nom du support ne peut-être vide.")
     * @Groups({"item.read", "item.write"})
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"item.read", "item.write"})
     */
    private $author;

    /**
     * @ORM\ManyToMany(targetEntity=Word::class)
     * @Groups({"item_words.read", "item_words.write"})
     * @ApiSubresource
     */
    private $words;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"item.read", "item.write"})
     */
    private $file;

    /**
     * @ORM\Column(type="integer", nullable=true, options={"default" : "0"}))
     * @Groups({"item.read"})
     */
    private $wordsNumber;


    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime", options={"default":"2020-01-01 00:00:00"})
     * @Gedmo\Timestampable(on="create")
     * @Groups({"item.read"})
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime", options={"default":"2020-01-01 00:00:00"})
     * @Gedmo\Timestampable
     * @Groups({"item.read"})
     */
    private $updatedAt;


    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ----------------------------------------GETTER AND SETTER------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */


    public function __construct()
    {
        $this->words = new ArrayCollection();
        $this->id = Uuid::uuid4();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getAuthor(): ?string
    {
        return $this->author;
    }

    public function setAuthor(?string $author): self
    {
        $this->author = $author;

        return $this;
    }

    /**
     * @return Collection|Word[]
     */
    public function getWords(): Collection
    {
        return $this->words;
    }


    public function addWord(Word $word): self
    {
        if(!$this->words->contains($word))
        {
            $this->words[] = $word;
        }

        return $this;
    }

    public function removeWord(Word $word): self
    {
        $this->words->removeElement($word);

        return $this;
    }

    public function getFile(): ?string
    {
        return $this->file;
    }

    public function setFile(?string $file): self
    {
        $this->file = $file;

        return $this;
    }

    public function getWordsNumber(): ?int
    {
        return $this->wordsNumber;
    }

    public function setWordsNumber(?int $wordsNumber): self
    {
        $this->wordsNumber = $wordsNumber;

        return $this;
    }


    /**
     * @return \DateTime
     */
    public function getCreatedAt(): \DateTime
    {
        return $this->createdAt;
    }

    /**
     * @param \DateTime $createdAt
     * @return Quote
     */
    public function setCreatedAt(\DateTime $createdAt): Quote
    {
        $this->createdAt = $createdAt;
        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getUpdatedAt(): \DateTime
    {
        return $this->updatedAt;
    }

    /**
     * @param \DateTime $updatedAt
     * @return Quote
     */
    public function setUpdatedAt(\DateTime $updatedAt): Quote
    {
        $this->updatedAt = $updatedAt;
        return $this;
    }


}

和单词

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\WordRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Serializer\Annotation\Groups;


/**
 * @ORM\Entity
 * @ApiResource(attributes={"order"={"value": "ASC"}})
 * @ORM\Table(name="`words`")
 */
class Word
{
    /**
     *
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     */
    private $id;

    /**
     * @Groups({"item_words.read"}),
     * @ORM\Column(type="string", length=255)
     */
    private $value;


    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ----------------------------------------GETTER AND SETTER------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */
    /* ---------------------------------------------------------------------------------------------- */



    public function __construct()
    {
        $this->id = Uuid::uuid4();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getValue(): ?string
    {
        return $this->value;
    }

    public function setValue(string $value): self
    {
        $this->value = $value;

        return $this;
    }


}

挖掘一段时间后,要使其正常工作,"method" 必须存在于操作中

他是工作注解

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"item.read"}},
 *     denormalizationContext={"groups"={"item.write"}},
 *     collectionOperations={"get", "post"},
 *     itemOperations={
 *          "get"={
 *              "method"="GET",
 *              "normalization_context"={"groups"={"item.read","item_words.read"}},
 *          },
 *          "put"={
 *              "method"="PUT",
 *              "normalization_context"={"groups"={"item.write", "item_words.write"}},
 *          },
 *         "delete",
 *         "getItemVocabulary"={
 *                   "method"="GET",
 *                   "path"="/items/{id}/vocabulary",
 *                   "controller"=ItemVocabularyController::class,
 *                  }
 *     }
 * )
 * @ORM\Entity
 * @ORM\Table(name="`items`")
 */