src/Entity/Product/ProductCategory.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Product;
  3. use App\Repository\Product\ProductCategoryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClassProductCategoryRepository::class)]
  8. class ProductCategory
  9. {
  10.     #[ORM\Id]
  11.     #[ORM\GeneratedValue]
  12.     #[ORM\Column]
  13.     private ?int $id null;
  14.     #[ORM\Column(length255)]
  15.     private ?string $name null;
  16.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'productCategories')]
  17.     private ?self $parent null;
  18.     #[ORM\OneToMany(mappedBy'parent'targetEntityself::class)]
  19.     private Collection $productCategories;
  20.     #[ORM\ManyToMany(targetEntityProduct::class, mappedBy'categories')]
  21.     private Collection $products;
  22.     public function __construct()
  23.     {
  24.         $this->productCategories = new ArrayCollection();
  25.         $this->products = new ArrayCollection();
  26.     }
  27.     public function __toString()
  28.     {
  29.         return $this->name;
  30.     }
  31.     public function getId(): ?int
  32.     {
  33.         return $this->id;
  34.     }
  35.     public function getName(): ?string
  36.     {
  37.         return $this->name;
  38.     }
  39.     public function setName(string $name): self
  40.     {
  41.         $this->name $name;
  42.         return $this;
  43.     }
  44.     public function getParent(): ?self
  45.     {
  46.         return $this->parent;
  47.     }
  48.     public function setParent(?self $parent): static
  49.     {
  50.         $this->parent $parent;
  51.         return $this;
  52.     }
  53.     /**
  54.      * @return Collection<int, self>
  55.      */
  56.     public function getProductCategories(): Collection
  57.     {
  58.         return $this->productCategories;
  59.     }
  60.     public function addProductCategory(self $productCategory): static
  61.     {
  62.         if (!$this->productCategories->contains($productCategory)) {
  63.             $this->productCategories->add($productCategory);
  64.             $productCategory->setParent($this);
  65.         }
  66.         return $this;
  67.     }
  68.     public function removeProductCategory(self $productCategory): static
  69.     {
  70.         if ($this->productCategories->removeElement($productCategory)) {
  71.             // set the owning side to null (unless already changed)
  72.             if ($productCategory->getParent() === $this) {
  73.                 $productCategory->setParent(null);
  74.             }
  75.         }
  76.         return $this;
  77.     }
  78.     /**
  79.      * @return Collection<int, Product>
  80.      */
  81.     public function getProducts(): Collection
  82.     {
  83.         return $this->products;
  84.     }
  85.     public function addProduct(Product $product): static
  86.     {
  87.         if (!$this->products->contains($product)) {
  88.             $this->products->add($product);
  89.             $product->addCategory($this);
  90.         }
  91.         return $this;
  92.     }
  93.     public function removeProduct(Product $product): static
  94.     {
  95.         if ($this->products->removeElement($product)) {
  96.             $product->removeCategory($this);
  97.         }
  98.         return $this;
  99.     }
  100. }