src/Entity/Category.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Model\Entity;
  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. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  9. class Category extends Entity
  10. {
  11.     #[ORM\Column(length255)]
  12.     private ?string $name null;
  13.     #[ORM\OneToMany(mappedBy'category'targetEntityProduct::class)]
  14.     private Collection $products;
  15.     public function __construct(string $name null)
  16.     {
  17.         $this->name $name;
  18.         $this->products = new ArrayCollection();
  19.     }
  20.     public function getName(): ?string
  21.     {
  22.         return $this->name;
  23.     }
  24.     public function setName(string $name): self
  25.     {
  26.         $this->name $name;
  27.         return $this;
  28.     }
  29.     /**
  30.      * @return Collection<int, Product>
  31.      */
  32.     public function getProducts(): Collection
  33.     {
  34.         return $this->products;
  35.     }
  36.     public function addProduct(Product $product): self
  37.     {
  38.         if (!$this->products->contains($product)) {
  39.             $this->products->add($product);
  40.             $product->setCategory($this);
  41.         }
  42.         return $this;
  43.     }
  44.     public function removeProduct(Product $product): self
  45.     {
  46.         if ($this->products->removeElement($product)) {
  47.             // set the owning side to null (unless already changed)
  48.             if ($product->getCategory() === $this) {
  49.                 $product->setCategory(null);
  50.             }
  51.         }
  52.         return $this;
  53.     }
  54.     public function __toString(): string
  55.     {
  56.         return $this->name;
  57.     }
  58. }