src/Entity/Category.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\CategoryTypeEnum;
  4. use App\Repository\CategoryRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  10. class Category extends BaseEntity
  11. {
  12.     #[ORM\Id]
  13.     #[ORM\GeneratedValue]
  14.     #[ORM\Column]
  15.     private ?int $id null;
  16.     #[ORM\Column(length255)]
  17.     #[Assert\NotBlank]
  18.     private ?string $name null;
  19.     #[ORM\Column]
  20.     #[Assert\Range(min1max10)]
  21.     private ?int $priority 1;
  22.     #[ORM\Column(length255nullabletrue)]
  23.     private ?string $color null;
  24.     #[ORM\Column(type'string'enumTypeCategoryTypeEnum::class)]
  25.     private CategoryTypeEnum $type CategoryTypeEnum::PHYSICAL;
  26.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'children')]
  27.     #[ORM\JoinColumn(onDelete'SET NULL')]
  28.     private ?self $parent null;
  29.     #[ORM\OneToMany(mappedBy'parent'targetEntityself::class)]
  30.     private Collection $children;
  31.     #[ORM\Column(length255nullabletrue)]
  32.     private ?string $image null;
  33.     #[ORM\ManyToMany(targetEntityProduct::class, mappedBy'categories')]
  34.     private Collection $products;
  35.     public function __construct()
  36.     {
  37.         $this->children = new ArrayCollection();
  38.         $this->products = new ArrayCollection();
  39.     }
  40.     public function __toString(): string
  41.     {
  42.         return $this->name ?? 'New Category';
  43.     }
  44.     public function getId(): ?int
  45.     {
  46.         return $this->id;
  47.     }
  48.     public function getName(): ?string
  49.     {
  50.         return $this->name;
  51.     }
  52.     public function setName(string $name): self
  53.     {
  54.         $this->name $name;
  55.         return $this;
  56.     }
  57.     public function getPriority(): ?int
  58.     {
  59.         return $this->priority;
  60.     }
  61.     public function setPriority(int $priority): self
  62.     {
  63.         $this->priority $priority;
  64.         return $this;
  65.     }
  66.     public function getColor(): ?string
  67.     {
  68.         return $this->color;
  69.     }
  70.     public function setColor(?string $color): self
  71.     {
  72.         $this->color $color;
  73.         return $this;
  74.     }
  75.     public function getType(): CategoryTypeEnum
  76.     {
  77.         return $this->type;
  78.     }
  79.     public function setType(CategoryTypeEnum $type): self
  80.     {
  81.         $this->type $type;
  82.         return $this;
  83.     }
  84.     public function getParent(): ?self
  85.     {
  86.         return $this->parent;
  87.     }
  88.     public function setParent(?self $parent): self
  89.     {
  90.         $this->parent $parent;
  91.         return $this;
  92.     }
  93.     /**
  94.      * @return Collection<int, self>
  95.      */
  96.     public function getChildren(): Collection
  97.     {
  98.         return $this->children;
  99.     }
  100.     public function addChild(self $child): self
  101.     {
  102.         if (!$this->children->contains($child)) {
  103.             $this->children->add($child);
  104.             $child->setParent($this);
  105.         }
  106.         return $this;
  107.     }
  108.     public function removeChild(self $child): self
  109.     {
  110.         if ($this->children->removeElement($child)) {
  111.             // set the owning side to null (unless already changed)
  112.             if ($child->getParent() === $this) {
  113.                 $child->setParent(null);
  114.             }
  115.         }
  116.         return $this;
  117.     }
  118.     public function getImage(): ?string
  119.     {
  120.         return $this->image;
  121.     }
  122.     public function setImage(?string $image): self
  123.     {
  124.         $this->image $image;
  125.         return $this;
  126.     }
  127.     /**
  128.      * @return Collection<int, Product>
  129.      */
  130.     public function getProducts(): Collection
  131.     {
  132.         return $this->products;
  133.     }
  134.     public function addProduct(Product $product): self
  135.     {
  136.         if (!$this->products->contains($product)) {
  137.             $this->products->add($product);
  138.             $product->addCategory($this);
  139.         }
  140.         return $this;
  141.     }
  142.     public function removeProduct(Product $product): self
  143.     {
  144.         if ($this->products->removeElement($product)) {
  145.             $product->removeCategory($this);
  146.         }
  147.         return $this;
  148.     }
  149. }