src/Entity/Category.php line 15

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. use Symfony\Component\Serializer\Annotation\Groups;
  8. /**
  9.  * @ORM\Entity(repositoryClass=CategoryRepository::class)
  10.  */
  11. class Category
  12. {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      * @Groups({"category_read"})
  18.      */
  19.     private $id;
  20.     /**
  21.      * @Groups({"category_read"})
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $name;
  25.     /**
  26.      * @Groups({"category_read"})
  27.      * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="categories")
  28.      */
  29.     private $parent;
  30.     /**
  31.      * @ORM\OneToMany(targetEntity=Category::class, mappedBy="parent")
  32.      */
  33.     private $categories;
  34.     /**
  35.      * @ORM\Column(type="datetime_immutable")
  36.      */
  37.     private $createdAt;
  38.     /**
  39.      * @ORM\Column(type="datetime_immutable")
  40.      */
  41.     private $updatedAt;
  42.     /**
  43.      * @ORM\OneToMany(targetEntity=Product::class, mappedBy="category")
  44.      */
  45.     private $products;
  46.     public function __construct()
  47.     {
  48.         $this->categories = new ArrayCollection();
  49.         $this->products = new ArrayCollection();
  50.     }
  51.     public function getId(): ?int
  52.     {
  53.         return $this->id;
  54.     }
  55.     public function getName(): ?string
  56.     {
  57.         return $this->name;
  58.     }
  59.     public function setName(string $name): self
  60.     {
  61.         $this->name $name;
  62.         return $this;
  63.     }
  64.     public function getParent(): ?self
  65.     {
  66.         return $this->parent;
  67.     }
  68.     public function setParent(?self $parent): self
  69.     {
  70.         $this->parent $parent;
  71.         return $this;
  72.     }
  73.     /**
  74.      * @return Collection<int, self>
  75.      */
  76.     public function getCategories(): Collection
  77.     {
  78.         return $this->categories;
  79.     }
  80.     public function addCategory(self $category): self
  81.     {
  82.         if (!$this->categories->contains($category)) {
  83.             $this->categories[] = $category;
  84.             $category->setParent($this);
  85.         }
  86.         return $this;
  87.     }
  88.     public function removeCategory(self $category): self
  89.     {
  90.         if ($this->categories->removeElement($category)) {
  91.             // set the owning side to null (unless already changed)
  92.             if ($category->getParent() === $this) {
  93.                 $category->setParent(null);
  94.             }
  95.         }
  96.         return $this;
  97.     }
  98.     public function getCreatedAt(): ?\DateTimeImmutable
  99.     {
  100.         return $this->createdAt;
  101.     }
  102.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  103.     {
  104.         $this->createdAt $createdAt;
  105.         return $this;
  106.     }
  107.     public function getUpdatedAt(): ?\DateTimeImmutable
  108.     {
  109.         return $this->updatedAt;
  110.     }
  111.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  112.     {
  113.         $this->updatedAt $updatedAt;
  114.         return $this;
  115.     }
  116.     /**
  117.      * @return Collection<int, Product>
  118.      */
  119.     public function getProducts(): Collection
  120.     {
  121.         return $this->products;
  122.     }
  123.     public function addProduct(Product $product): self
  124.     {
  125.         if (!$this->products->contains($product)) {
  126.             $this->products[] = $product;
  127.             $product->setCategory($this);
  128.         }
  129.         return $this;
  130.     }
  131.     public function removeProduct(Product $product): self
  132.     {
  133.         if ($this->products->removeElement($product)) {
  134.             // set the owning side to null (unless already changed)
  135.             if ($product->getCategory() === $this) {
  136.                 $product->setCategory(null);
  137.             }
  138.         }
  139.         return $this;
  140.     }
  141. }