src/Entity/Category.php line 13

  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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  9. #[UniqueEntity(fields: ['category_name'], message'Il existe déjà une catégorie avec ce nom.')]
  10. class Category
  11. {
  12.     #[ORM\Id]
  13.     #[ORM\GeneratedValue]
  14.     #[ORM\Column]
  15.     private ?int $id null;
  16.     #[ORM\Column(length255uniquetrue)]
  17.     private ?string $category_name null;
  18.     #[ORM\OneToMany(mappedBy'product_category'targetEntityProduct::class)]
  19.     private Collection $products;
  20.     public function __construct()
  21.     {
  22.         $this->products = new ArrayCollection();
  23.     }
  24.     public function getId(): ?int
  25.     {
  26.         return $this->id;
  27.     }
  28.     public function getCategoryName(): ?string
  29.     {
  30.         return $this->category_name;
  31.     }
  32.     public function setCategoryName(string $category_name): self
  33.     {
  34.         $this->category_name $category_name;
  35.         return $this;
  36.     }
  37.     /**
  38.      * @return Collection<int, Product>
  39.      */
  40.     public function getProducts(): Collection
  41.     {
  42.         return $this->products;
  43.     }
  44.     public function addProduct(Product $product): self
  45.     {
  46.         if (!$this->products->contains($product)) {
  47.             $this->products->add($product);
  48.             $product->setProductCategory($this);
  49.         }
  50.         return $this;
  51.     }
  52.     public function removeProduct(Product $product): self
  53.     {
  54.         if ($this->products->removeElement($product)) {
  55.             // set the owning side to null (unless already changed)
  56.             if ($product->getProductCategory() === $this) {
  57.                 $product->setProductCategory(null);
  58.             }
  59.         }
  60.         return $this;
  61.     }
  62.     public function __toString(): string
  63.     {
  64.         return $this->category_name;
  65.     }
  66. }