src/Entity/Category.php line 20

  1. <?php
  2. /*
  3.  * This file is part of the Doctrine-TestSet project created by
  4.  * https://github.com/MacFJA
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * at https://github.com/MacFJA/Doctrine-TestSet
  8.  */
  9. namespace App\Entity;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Doctrine\ORM\PersistentCollection;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. #[ORM\Table(name"category")]
  15. #[ORM\Entity]
  16. class Category
  17. {
  18.     #[ORM\Column(name"id"type"integer")]
  19.     #[ORM\Id]
  20.     #[ORM\GeneratedValue(strategy"AUTO")]
  21.     #[Groups(["products"])]
  22.     protected $id null;
  23.     #[ORM\Column(type"string")]
  24.     #[Groups(["products"])]
  25.     protected $name;
  26.     #[ORM\ManyToMany(targetEntity"Product"mappedBy"categories")]
  27.     protected $products;
  28.     #[ORM\ManyToOne(targetEntity"Category")]
  29.     #[ORM\JoinColumn(name"parent_id"referencedColumnName"id"nullabletrue)]
  30.     #[Groups(["products"])]
  31.     protected $parent;
  32.     public function __construct()
  33.     {
  34.         $this->products = new ArrayCollection();
  35.     }
  36.     /**
  37.      * @return string
  38.      */
  39.     public function __toString()
  40.     {
  41.         return $this->getName() ?? '';
  42.     }
  43.     /**
  44.      * Get the id of the category.
  45.      * Return null if the category is new and not saved.
  46.      *
  47.      * @return int|null
  48.      */
  49.     public function getId(): ?int
  50.     {
  51.         return $this->id;
  52.     }
  53.     /**
  54.      * Set the name of the category.
  55.      *
  56.      * @param string $name
  57.      */
  58.     public function setName($name): void
  59.     {
  60.         $this->name $name;
  61.     }
  62.     /**
  63.      * Get the name of the category.
  64.      *
  65.      * @return string
  66.      */
  67.     public function getName(): string
  68.     {
  69.         return $this->name;
  70.     }
  71.     /**
  72.      * Set the parent category.
  73.      *
  74.      * @param Category $parent
  75.      */
  76.     public function setParent($parent): void
  77.     {
  78.         $this->parent $parent;
  79.     }
  80.     /**
  81.      * Get the parent category.
  82.      *
  83.      * @return Category
  84.      */
  85.     public function getParent(): ?Category
  86.     {
  87.         return $this->parent;
  88.     }
  89.     /**
  90.      * Return all product associated to the category.
  91.      *
  92.      * @return ArrayCollection|array
  93.      */
  94.     public function getProducts(): ArrayCollection|array|PersistentCollection
  95.     {
  96.         return $this->products;
  97.     }
  98.     /**
  99.      * Set all products in the category.
  100.      *
  101.      * @param Product[] $products
  102.      */
  103.     public function setProducts($products): void
  104.     {
  105.         $this->products->clear();
  106.         $this->products = new ArrayCollection($products);
  107.     }
  108.     /**
  109.      * Add a product in the category.
  110.      *
  111.      * @param $product Product The product to associate
  112.      */
  113.     public function addProduct($product): void
  114.     {
  115.         if ($this->products->contains($product)) {
  116.             return;
  117.         }
  118.         $this->products->add($product);
  119.         $product->addCategory($this);
  120.     }
  121.     /**
  122.      * @param Product $product
  123.      */
  124.     public function removeProduct($product): void
  125.     {
  126.         if (!$this->products->contains($product)) {
  127.             return;
  128.         }
  129.         $this->products->removeElement($product);
  130.         $product->removeCategory($this);
  131.     }
  132. }