Describe entities as API Platform resources

This commit is contained in:
2024-06-05 17:37:53 +02:00
parent 48bc5fd8c3
commit 22be991b8c
16 changed files with 621 additions and 1 deletions

View File

@@ -2,32 +2,45 @@
namespace App\Entity;
use ApiPlatform\Metadata;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\SpeciesRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity(repositoryClass: SpeciesRepository::class)]
#[ApiResource(
operations: [new Metadata\Post(), new Metadata\Get(), new Metadata\Put(), new Metadata\Delete(), new Metadata\Patch()],
normalizationContext: ['groups' => ['species:collection:read', 'species:read']],
)]
#[Metadata\GetCollection(normalizationContext: ['groups' => ['species:collection:read']])]
class Species
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['species:collection:read'])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['species:collection:read'])]
private ?string $scientific_name = null;
#[ORM\Column(length: 255)]
#[Groups(['species:collection:read'])]
private ?string $vernacular_name = null;
#[ORM\Column(length: 255)]
#[Groups(['species:collection:read'])]
private ?string $region = null;
/**
* @var Collection<int, Post>
*/
#[ORM\OneToMany(targetEntity: Post::class, mappedBy: 'species')]
#[Groups(['species:read'])]
private Collection $posts;
public function __construct()