src/Entity/Products.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Trait\CreatedAtTrait;
  4. use App\Entity\Trait\SlugTrait;
  5. use App\Repository\ProductsRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. #[ORM\Entity(repositoryClass: ProductsRepository::class)]
  12. class Products
  13. {
  14.     use CreatedAtTrait;
  15.     use SlugTrait;
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     private ?int $id = null;
  20.     #[ORM\Column(length: 255)]
  21.     #[Assert\NotBlank(message: 'Le nom du produit ne peut pas être vide')]
  22.     #[Assert\Length(
  23.         min:8, 
  24.         max:200, 
  25.         minMessage: 'Le titre doit faire au moins {{ limit }} caractères',
  26.         maxMessage: 'Le titre ne doit pas faire plus de {{ limit }} caractères'
  27.         )]
  28.     private ?string $name = null;
  29.     #[ORM\Column(type: Types::TEXT)]
  30.     private ?string $description = null;
  31.     #[ORM\Column]
  32.     private ?int $price = null;
  33.     #[ORM\Column]
  34.     #[Assert\PositiveOrZero(message: 'Le stock ne peut pas être négatif')]
  35.     private ?int $stock = null;
  36.     #[ORM\ManyToOne(inversedBy: 'products')]
  37.     #[ORM\JoinColumn(nullable: false)]
  38.     private ?Categories $categories = null;
  39.     #[ORM\OneToMany(mappedBy: 'products', targetEntity: Images::class, orphanRemoval: true, cascade:['persist'])]
  40.     private Collection $images;
  41.     #[ORM\OneToMany(mappedBy: 'products', targetEntity: OrdersDetails::class)]
  42.     private Collection $ordersDetails;
  43.     public function __construct()
  44.     {
  45.         $this->images = new ArrayCollection();
  46.         $this->ordersDetails = new ArrayCollection();
  47.         $this->created_at = new \DateTimeImmutable(); 
  48.     }
  49.     public function getId(): ?int
  50.     {
  51.         return $this->id;
  52.     }
  53.     public function getName(): ?string
  54.     {
  55.         return $this->name;
  56.     }
  57.     public function setName(string $name): self
  58.     {
  59.         $this->name = $name;
  60.         return $this;
  61.     }
  62.     public function getDescription(): ?string
  63.     {
  64.         return $this->description;
  65.     }
  66.     public function setDescription(string $description): self
  67.     {
  68.         $this->description = $description;
  69.         return $this;
  70.     }
  71.     public function getPrice(): ?int
  72.     {
  73.         return $this->price;
  74.     }
  75.     public function setPrice(int $price): self
  76.     {
  77.         $this->price = $price;
  78.         return $this;
  79.     }
  80.     public function getStock(): ?int
  81.     {
  82.         return $this->stock;
  83.     }
  84.     public function setStock(int $stock): self
  85.     {
  86.         $this->stock = $stock;
  87.         return $this;
  88.     }
  89.     public function getCategories(): ?Categories
  90.     {
  91.         return $this->categories;
  92.     }
  93.     public function setCategories(?Categories $categories): self
  94.     {
  95.         $this->categories = $categories;
  96.         return $this;
  97.     }
  98.     /**
  99.      * @return Collection<int, Images>
  100.      */
  101.     public function getImages(): Collection
  102.     {
  103.         return $this->images;
  104.     }
  105.     public function addImage(Images $image): self
  106.     {
  107.         if (!$this->images->contains($image)) {
  108.             $this->images->add($image);
  109.             $image->setProducts($this);
  110.         }
  111.         return $this;
  112.     }
  113.     public function removeImage(Images $image): self
  114.     {
  115.         if ($this->images->removeElement($image)) {
  116.             // set the owning side to null (unless already changed)
  117.             if ($image->getProducts() === $this) {
  118.                 $image->setProducts(null);
  119.             }
  120.         }
  121.         return $this;
  122.     }
  123.     /**
  124.      * @return Collection<int, OrdersDetails>
  125.      */
  126.     public function getOrdersDetails(): Collection
  127.     {
  128.         return $this->ordersDetails;
  129.     }
  130.     public function addOrdersDetail(OrdersDetails $ordersDetail): self
  131.     {
  132.         if (!$this->ordersDetails->contains($ordersDetail)) {
  133.             $this->ordersDetails->add($ordersDetail);
  134.             $ordersDetail->setProducts($this);
  135.         }
  136.         return $this;
  137.     }
  138.     public function removeOrdersDetail(OrdersDetails $ordersDetail): self
  139.     {
  140.         if ($this->ordersDetails->removeElement($ordersDetail)) {
  141.             // set the owning side to null (unless already changed)
  142.             if ($ordersDetail->getProducts() === $this) {
  143.                 $ordersDetail->setProducts(null);
  144.             }
  145.         }
  146.         return $this;
  147.     }
  148. }