src/Entity/Image.php line 12

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ImageRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. #[ORM\Entity(repositoryClassImageRepository::class)]
  7. #[UniqueEntity(fields: ['image_name'], message'Il existe déjà une image avec ce nom.')]
  8. #[ORM\HasLifecycleCallbacks]
  9. class Image
  10. {
  11.     #[ORM\Id]
  12.     #[ORM\GeneratedValue]
  13.     #[ORM\Column]
  14.     private ?int $id null;
  15.     #[ORM\Column(length255uniquetrue)]
  16.     private ?string $image_name null;
  17.     #[ORM\ManyToOne(inversedBy'images')]
  18.     #[ORM\JoinColumn(nullablefalse)]
  19.     private ?Product $image_product null;
  20.     public function getId(): ?int
  21.     {
  22.         return $this->id;
  23.     }
  24.     public function getImageName(): ?string
  25.     {
  26.         return $this->image_name;
  27.     }
  28.     public function setImageName(string $image_name): self
  29.     {
  30.         $this->image_name $image_name;
  31.         return $this;
  32.     }
  33.     public function getImageProduct(): ?Product
  34.     {
  35.         return $this->image_product;
  36.     }
  37.     public function setImageProduct(?Product $image_product): self
  38.     {
  39.         $this->image_product $image_product;
  40.         return $this;
  41.     }
  42.     #[ORM\PreRemove]
  43.     public function preRemove(): void
  44.     {
  45.         $imagePath 'assets/img/product/' $this->getImageName();
  46.         if (file_exists($imagePath)) {
  47.             unlink($imagePath);
  48.         }
  49.     }
  50. }