Describe entities as API Platform resources
This commit is contained in:
@@ -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()
|
||||
|
Reference in New Issue
Block a user