src/Entity/Category.php line 17

  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Trait\MainTranslationTrait;
  4. use App\Entity\Translation\CategoryTranslation;
  5. use App\Entity\Translation\ProductTranslation;
  6. use App\Repository\CategoryRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Translatable\Translatable;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. #[Gedmo\TranslationEntity(class: self::TRANSLATION_ENTITY)]
  13. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  14. class Category implements Translatable
  15. {
  16.     use MainTranslationTrait;
  17.     const TRANSLATION_ENTITY CategoryTranslation::class;
  18.     public function __toString(): string
  19.     {
  20.         return $this->title ?? '';
  21.     }
  22.     #[ORM\Id]
  23.     #[ORM\GeneratedValue]
  24.     #[ORM\Column]
  25.     private ?int $id null;
  26.     #[ORM\Column(type'string',nullabletrue)]
  27.     #[Gedmo\Translatable]
  28.     private ?string $title null;
  29.     #[ORM\OneToMany(mappedBy'category'targetEntityProduct::class)]
  30.     private Collection $products;
  31.     public function __construct()
  32.     {
  33.         $this->products = new ArrayCollection();
  34.     }
  35.     public function getId(): ?int
  36.     {
  37.         return $this->id;
  38.     }
  39.     public function getProducts(): Collection
  40.     {
  41.         return $this->products;
  42.     }
  43.     public function addProduct(Product $product): self
  44.     {
  45.         if (!$this->products->contains($product)) {
  46.             $this->products->add($product);
  47.             $product->setCategory($this);
  48.         }
  49.         return $this;
  50.     }
  51.     public function removeProduct(Product $product): self
  52.     {
  53.         if ($this->products->removeElement($product)) {
  54.             if ($product->getCategory() === $this) {
  55.                 $product->setCategory(null);
  56.             }
  57.         }
  58.         return $this;
  59.     }
  60.     public function getTitle(): ?string
  61.     {
  62.         return $this->title;
  63.     }
  64.     public function setTitle(?string $title): self
  65.     {
  66.         $this->title $title;
  67.         return $this;
  68.     }
  69. }