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\OneToMany(targetEntity=Product::class, mappedBy="category")
  24. */
  25. private $products;
  26. public function __construct()
  27. {
  28. $this->products = new ArrayCollection();
  29. }
  30. public function __toString(){
  31. return $this->getName();
  32. }
  33. public function getId(): ?int
  34. {
  35. return $this->id;
  36. }
  37. public function getName(): ?string
  38. {
  39. return $this->name;
  40. }
  41. public function setName(string $name): self
  42. {
  43. $this->name = $name;
  44. return $this;
  45. }
  46. /**
  47. * @ORM\Column(type="string", length=255, nullable=true)
  48. */
  49. private $image;
  50. public function getImage(): ?string
  51. {
  52. return $this->image;
  53. }
  54. public function setImage(?string $image): self
  55. {
  56. $this->image = $image;
  57. return $this;
  58. }
  59. /**
  60. * @ORM\Column(type="text", nullable=true)
  61. */
  62. private $description;
  63. public function getDescription(): ?string
  64. {
  65. return $this->description;
  66. }
  67. public function setDescription(?string $description): self
  68. {
  69. $this->description = $description;
  70. return $this;
  71. }
  72. /**
  73. * @return Collection<int, Product>
  74. */
  75. public function getProducts(): Collection
  76. {
  77. return $this->products;
  78. }
  79. public function addProduct(Product $product): self
  80. {
  81. if (!$this->products->contains($product)) {
  82. $this->products[] = $product;
  83. $product->setCategory($this);
  84. }
  85. return $this;
  86. }
  87. public function removeProduct(Product $product): self
  88. {
  89. if ($this->products->removeElement($product)) {
  90. // set the owning side to null (unless already changed)
  91. if ($product->getCategory() === $this) {
  92. $product->setCategory(null);
  93. }
  94. }
  95. return $this;
  96. }
  97. }