src/Entity/Article.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ArticleRepository;
  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 App\Entity\Categorie;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. #[ORM\Entity(repositoryClassArticleRepository::class)]
  11. class Article
  12. {
  13.     #[ORM\Id]
  14.     #[ORM\GeneratedValue]
  15.     #[ORM\Column]
  16.     private ?int $id null;
  17.     #[ORM\Column(length255)]
  18.     private ?string $title null;
  19.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  20.     #[Assert\NotBlank(message"La date de création ne peut pas être vide.")]
  21.     private ?\DateTimeInterface $creationDate null;
  22.     #[ORM\ManyToOne(inversedBy'haves')]
  23.     #[ORM\JoinColumn(nullablefalse)]
  24.     private ?Categorie $categorie null;
  25.     #[ORM\ManyToOne(inversedBy'articles')]
  26.     #[ORM\JoinColumn(nullablefalse)]
  27.     private ?User $user null;
  28.  #[ORM\OneToMany(targetEntityPost::class, mappedBy'article',cascade: ["remove"], orphanRemovaltrue)]
  29.     private Collection $belongs;
  30.     public function __construct()
  31.     {
  32.         $this->belongs = new ArrayCollection();
  33.         $this->creationDate = new \DateTime();
  34.     }
  35.     public function getId(): ?int
  36.     {
  37.         return $this->id;
  38.     }
  39.     public function getTitle(): ?string
  40.     {
  41.         return $this->title;
  42.     }
  43.     public function setTitle(string $title): static
  44.     {
  45.         $this->title $title;
  46.         return $this;
  47.     }
  48.     public function getCreationDate(): ?\DateTimeInterface
  49.     {
  50.         return $this->creationDate;
  51.     }
  52.     public function setCreationDate(\DateTimeInterface $creationDate): static
  53.     {
  54.         $this->creationDate $creationDate;
  55.         return $this;
  56.     }
  57. #permet de gérer la relation 1 à plusieurs entre entité Article et POST qui est une Collections d'objet
  58.     /**
  59.      * @return Collection<int, Post>
  60.      */
  61.     public function getBelongs(): Collection
  62.     {
  63.         return $this->belongs;
  64.     }
  65. #Ajoute un objet Post à la collection
  66.     public function addBelong(Post $belong): static
  67.     {
  68.         //vérifie si ds collection y a un objet type POST
  69.         if (!$this->belongs->contains($belong)) {
  70.             $this->belongs->add($belong);
  71.             $belong->setArticle($this);
  72.         }
  73.         return $this;
  74.     }
  75. #Supprime un objet Post à la collection
  76.     public function removeBelong(Post $belong): static
  77.     {
  78.         if ($this->belongs->removeElement($belong)) {
  79.             if ($belong->getArticle() === $this) {
  80.                 $belong->setArticle(null);
  81.             }
  82.         }
  83.         return $this;
  84.     }
  85.     public function getCategorie(): ?Categorie
  86.     {
  87.         return $this->categorie;
  88.     }
  89.     public function setCategorie(?Categorie $categorie): static
  90.     {
  91.         $this->categorie $categorie;
  92.         return $this;
  93.     }
  94.     public function getUser(): ?User
  95.     {
  96.         return $this->user;
  97.     }
  98.     public function setUser(?User $user): static
  99.     {
  100.         $this->user $user;
  101.         return $this;
  102.     }
  103. }