I have 2 entities:
I wanted to retrieve only words for itemOperation
and not for collectionOperations
so i added a group GET "item_words.read"
and for PUT denormalization_context
.
It is perfectly working for GET operation, for some reason the PUT operation is not giving me the “words” subresource.
GET :
PUT :
Here is my code :
Item
JavaScript
x
<?php
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use ApiPlatformCoreAnnotationApiSubresource;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use RamseyUuidUuid;
use AppControllerFileUploadController;
use SymfonyComponentValidatorConstraints as Assert;
use AppControllerItemVocabularyController;
use SymfonyComponentSerializerAnnotationGroups;
use GedmoMappingAnnotation 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,
* }
* }
* )
* @ORMEntity
* @ORMTable(name="`items`")
*/
class Item
{
/**
*
* @ORMId
* @ORMColumn(type="uuid", unique=true)
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
* @AssertNotBlank(message="Le nom du support ne peut-être vide.")
* @Groups({"item.read", "item.write"})
*/
private $name;
/**
* @ORMColumn(type="string", length=255, nullable=true)
* @Groups({"item.read", "item.write"})
*/
private $author;
/**
* @ORMManyToMany(targetEntity=Word::class)
* @Groups({"item_words.read", "item_words.write"})
* @ApiSubresource
*/
private $words;
/**
* @ORMColumn(type="string", length=255, nullable=true)
* @Groups({"item.read", "item.write"})
*/
private $file;
/**
* @ORMColumn(type="integer", nullable=true, options={"default" : "0"}))
* @Groups({"item.read"})
*/
private $wordsNumber;
/**
* @var DateTime
*
* @ORMColumn(type="datetime", options={"default":"2020-01-01 00:00:00"})
* @GedmoTimestampable(on="create")
* @Groups({"item.read"})
*/
private $createdAt;
/**
* @var DateTime
*
* @ORMColumn(type="datetime", options={"default":"2020-01-01 00:00:00"})
* @GedmoTimestampable
* @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;
}
}
and Word
JavaScript
<?php
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use AppRepositoryWordRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use RamseyUuidUuid;
use SymfonyComponentSerializerAnnotationGroups;
/**
* @ORMEntity
* @ApiResource(attributes={"order"={"value": "ASC"}})
* @ORMTable(name="`words`")
*/
class Word
{
/**
*
* @ORMId
* @ORMColumn(type="uuid", unique=true)
*/
private $id;
/**
* @Groups({"item_words.read"}),
* @ORMColumn(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;
}
}
Advertisement
Answer
After digging for a while , to make it works the "method"
must be present on the operation
He is the working annotation
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,
* }
* }
* )
* @ORMEntity
* @ORMTable(name="`items`")
*/