src/Entity/Category.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8. * @ORM\Entity(repositoryClass=CategoryRepository::class)
  9. */
  10. class Category
  11. {
  12. /**
  13. * @ORM\Id
  14. * @ORM\GeneratedValue
  15. * @ORM\Column(type="integer")
  16. */
  17. private $id;
  18. /**
  19. * @ORM\Column(type="string", length=255)
  20. */
  21. private $name;
  22. /**
  23. * @ORM\Column(type="string", length=255)
  24. */
  25. private $slug;
  26. /**
  27. * @ORM\OneToMany(targetEntity=Product::class, mappedBy="category")
  28. */
  29. private $products;
  30. public function __construct()
  31. {
  32. $this->products = new ArrayCollection();
  33. }
  34. public function __toString()
  35. {
  36. return $this->name;
  37. }
  38. public function getId(): ?int
  39. {
  40. return $this->id;
  41. }
  42. public function getName(): ?string
  43. {
  44. return $this->name;
  45. }
  46. public function setName(string $name): self
  47. {
  48. $this->name = $name;
  49. return $this;
  50. }
  51. public function getSlug(): ?string
  52. {
  53. return $this->slug;
  54. }
  55. public function setSlug(string $slug): self
  56. {
  57. $this->slug = $slug;
  58. return $this;
  59. }
  60. /**
  61. * @return Collection<int, Product>
  62. */
  63. public function getProducts(): Collection
  64. {
  65. return $this->products;
  66. }
  67. public function addProduct(Product $product): self
  68. {
  69. if (!$this->products->contains($product)) {
  70. $this->products[] = $product;
  71. $product->setCategory($this);
  72. }
  73. return $this;
  74. }
  75. public function removeProduct(Product $product): self
  76. {
  77. if ($this->products->removeElement($product)) {
  78. // set the owning side to null (unless already changed)
  79. if ($product->getCategory() === $this) {
  80. $product->setCategory(null);
  81. }
  82. }
  83. return $this;
  84. }
  85. }