首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Api- itemOperations上denormalization_context组的PUT操作不起作用

Api- itemOperations上denormalization_context组的PUT操作不起作用
EN

Stack Overflow用户
提问于 2020-11-24 01:54:35
回答 1查看 542关注 0票数 0

我有两个实体:

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

它非常适合GET操作,由于某种原因,PUT操作没有给我提供“word”子资源。

获取:

放入:

下面是我的代码:

项目

代码语言:javascript
复制
<?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;
    }


}

和Word

代码语言:javascript
复制
<?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;
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-24 02:54:33

深入研究一段时间后,要使其正常工作,操作上必须存在"method"

他就是工作的注解

代码语言:javascript
复制
/**
 * @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`")
 */
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64973686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档