Author.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Get;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Doctrine\Common\Collections\Collection;
  8. #[ORM\Entity]
  9. #[ApiResource(operations: [
  10. new Get()
  11. ])]
  12. class Author
  13. {
  14. #[ORM\Id]
  15. #[ORM\GeneratedValue]
  16. #[ORM\Column]
  17. private ?int $id = null;
  18. #[ORM\Column(length: 100, nullable: false)]
  19. private string $name;
  20. #[ORM\OneToMany(targetEntity: Song::class, mappedBy: 'author', cascade: ['persist', 'remove'])]
  21. private Collection $songs;
  22. public function getId(): ?int
  23. {
  24. return $this->id;
  25. }
  26. public function setId(?int $id): self
  27. {
  28. $this->id = $id;
  29. return $this;
  30. }
  31. public function getName(): string
  32. {
  33. return $this->name;
  34. }
  35. public function setName(string $name): self
  36. {
  37. $this->name = $name;
  38. return $this;
  39. }
  40. public function getSongs(): Collection
  41. {
  42. return $this->songs;
  43. }
  44. public function addSong(Song $songs): self
  45. {
  46. if (!$this->songs->contains($songs)) {
  47. $this->songs[] = $songs;
  48. $songs->setAuthor($this);
  49. }
  50. return $this;
  51. }
  52. public function removeSong(Song $songs): self {
  53. $this->songs->removeElement($songs);
  54. return $this;
  55. }
  56. }