src/Entity/Category.php line 16

  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\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. #[ORM\Entity(repositoryClass:CategoryRepository::class)]
  12. #[Vich\Uploadable]
  13. class Category
  14. {
  15.     #[ORM\Id]
  16.     #[ORM\GeneratedValue]
  17.     #[ORM\Column]
  18.     private ?int $id null;
  19.     #[ORM\Column(length255)]
  20.     private ?string $name;
  21.     // NOTE: This is not a mapped field of entity metadata, just a simple property.
  22.     #[Vich\UploadableField(mapping:'category_image'fileNameProperty'image',originalName:'nomInitial')]
  23.     private ?File $imageFile null;
  24.     #[ORM\Column(length255nullabletrue)]
  25.     private ?string $image null;
  26.     #[ORM\ManyToOne(inversedBy'categories')]
  27.     private ?Service $service null;
  28.     #[ORM\OneToMany(mappedBy'category'targetEntityProduct::class)]
  29.     private Collection $products;
  30.     #[ORM\Column(length255nullabletrue)]
  31.     private ?string $nomInitial null;
  32.     #[Gedmo\Slug(fields: ['name'])]
  33.     #[ORM\Column(length255,uniquetrue)]
  34.     private ?string $slug null;
  35.     public function __construct()
  36.     {
  37.         $this->products = new ArrayCollection();
  38.         $this->createdAt = new \DateTimeImmutable('now');
  39.     }
  40.     public function __toString()
  41.     {
  42.         return $this->name;
  43.     }
  44.     public function getId(): ?int
  45.     {
  46.         return $this->id;
  47.     }
  48.     public function getName(): ?string
  49.     {
  50.         if (isset($this->name)) {
  51.             return $this->name;
  52.         }
  53.         return null;
  54.     }
  55.     public function setName(string $name): self
  56.     {
  57.         $this->name $name;
  58.         return $this;
  59.     }
  60.     public function getImage(): ?string
  61.     {
  62.         return $this->image;
  63.     }
  64.     public function setImage(?string $image): self
  65.     {
  66.         $this->image $image;
  67.         return $this;
  68.     }
  69.     public function getService(): ?Service
  70.     {
  71.         return $this->service;
  72.     }
  73.     public function setService(?Service $service): self
  74.     {
  75.         $this->service $service;
  76.         return $this;
  77.     }
  78.     /**
  79.      * @return Collection<int, Product>
  80.      */
  81.     public function getProducts(): Collection
  82.     {
  83.         return $this->products;
  84.     }
  85.     public function addProduct(Product $product): self
  86.     {
  87.         if (!$this->products->contains($product)) {
  88.             $this->products->add($product);
  89.             $product->setCategory($this);
  90.         }
  91.         return $this;
  92.     }
  93.     public function removeProduct(Product $product): self
  94.     {
  95.         if ($this->products->removeElement($product)) {
  96.             // set the owning side to null (unless already changed)
  97.             if ($product->getCategory() === $this) {
  98.                 $product->setCategory(null);
  99.             }
  100.         }
  101.         return $this;
  102.     }
  103.     /**
  104.      * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
  105.      * of 'UploadedFile' is injected into this setter to trigger the update. If this
  106.      * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
  107.      * must be able to accept an instance of 'File' as the bundle will inject one here
  108.      * during Doctrine hydration.
  109.      *
  110.      * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
  111.      */
  112.     public function setImageFile(?File $imageFile null): void
  113.     {
  114.         $this->imageFile $imageFile;
  115.         if (null !== $imageFile) {
  116.             // It is required that at least one field changes if you are using doctrine
  117.             // otherwise the event listeners won't be called and the file is lost
  118.             $this->updatedAt = new \DateTimeImmutable();
  119.         }
  120.     }
  121.     public function getImageFile(): ?File
  122.     {
  123.         return $this->imageFile;
  124.     }
  125.     public function getNomInitial(): ?string
  126.     {
  127.         return $this->nomInitial;
  128.     }
  129.     public function setNomInitial(string $nomInitialnull): self
  130.     {
  131.         $this->nomInitial $nomInitial;
  132.         return $this;
  133.     }
  134.     public function getSlug(): ?string
  135.     {
  136.         return $this->slug;
  137.     }
  138. }