src/Entity/Poll.php line 14

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\PollRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. #[ORM\Entity(repositoryClassPollRepository::class)]
  9. #[ORM\Table(name'polls')]
  10. #[ORM\HasLifecycleCallbacks]
  11. class Poll
  12. {
  13.     #[ORM\Id]
  14.     #[ORM\GeneratedValue]
  15.     #[ORM\Column(type'integer')]
  16.     private ?int $id null;
  17.     #[ORM\Column(type'string'length255)]
  18.     private ?string $title null;
  19.     #[ORM\Column(type'datetime')]
  20.     private ?\DateTimeInterface $createdAt null;
  21.     #[ORM\Column(type'datetime')]
  22.     private ?\DateTimeInterface $updatedAt null;
  23.     #[ORM\OneToMany(mappedBy'poll'targetEntity'PollQuestion'cascade: ['persist''remove'], orphanRemovaltrue)]
  24.     private Collection $questions;
  25.     public function __construct()
  26.     {
  27.         $this->questions = new ArrayCollection();
  28.         $this->setCreatedAt(new \DateTime());
  29.     }
  30.     public function __toString(): string
  31.     {
  32.         return $this->title;
  33.     }
  34.     public function getId(): ?int
  35.     {
  36.         return $this->id;
  37.     }
  38.     public function getTitle(): ?string
  39.     {
  40.         return $this->title;
  41.     }
  42.     public function setTitle(?string $title): self
  43.     {
  44.         $this->title $title;
  45.         return $this;
  46.     }
  47.     public function getCreatedAt(): ?\DateTimeInterface
  48.     {
  49.         return $this->createdAt;
  50.     }
  51.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  52.     {
  53.         $this->createdAt $createdAt;
  54.         return $this;
  55.     }
  56.     public function getUpdatedAt(): ?\DateTimeInterface
  57.     {
  58.         return $this->updatedAt;
  59.     }
  60.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  61.     {
  62.         $this->updatedAt $updatedAt;
  63.         return $this;
  64.     }
  65.     public function getQuestions(): Collection
  66.     {
  67.         return $this->questions;
  68.     }
  69.     public function setQuestions(Collection $questions): self
  70.     {
  71.         $this->questions $questions;
  72.         return $this;
  73.     }
  74.     public function addQuestion(PollQuestion $question): self
  75.     {
  76.         $question->setPoll($this);
  77.         $this->questions->add($question);
  78.         return $this;
  79.     }
  80.     public function removeQuestion(PollQuestion $question): self
  81.     {
  82.         $this->questions->removeElement($question);
  83.         return $this;
  84.     }
  85.     #[ORM\PrePersist]
  86.     #[ORM\PreUpdate]
  87.     public function updatedTimestamps(): void
  88.     {
  89.         $this->setUpdatedAt(new \DateTime());
  90.         if (null === $this->getCreatedAt()) {
  91.             $this->setCreatedAt(new \DateTime());
  92.         }
  93.     }
  94. }