src/Entity/Unite.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\TimeStampable;
  4. use App\Repository\UniteRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. #[ORM\Entity(repositoryClass: UniteRepository::class)]
  13. #[UniqueEntity(
  14.     fields: ['name',],
  15.     errorPath: 'name',
  16.     message: 'Le nom que vous avez saisi est déjà utilisé',
  17. )]
  18. #[ORM\HasLifecycleCallbacks]
  19. class Unite
  20. {
  21.     use TimeStampable;
  22.     
  23.     #[ORM\Id]
  24.     #[ORM\GeneratedValue]
  25.     #[ORM\Column]
  26.     #[Groups(["entity:write",])]
  27.     private ?int $id = null;
  28.     #[ORM\Column(length: 255)]
  29.     #[Groups(["entity:write", 'activity:read', 'entity:write', 'task:read'])]
  30.     #[Assert\NotBlank(message:"Veuillez remplir ce champ")]
  31.     private ?string $name = null;
  32.     #[ORM\Column(type: Types::TEXT)]
  33.     #[Groups(["entity:write", 'activity:read'])]
  34.     private ?string $description = null;
  35.     #[ORM\OneToMany(mappedBy: 'unite', targetEntity: Activity::class)]
  36.     private Collection $activities;
  37.     public function __construct(){
  38.         $this->createdAt = new \DateTimeImmutable();
  39.         $this->activities = new ArrayCollection();
  40.     }
  41.     public function getId(): ?int
  42.     {
  43.         return $this->id;
  44.     }
  45.     public function getName(): ?string
  46.     {
  47.         return $this->name;
  48.     }
  49.     public function setName(string $name): static
  50.     {
  51.         $this->name = $name;
  52.         return $this;
  53.     }
  54.     public function getDescription(): ?string
  55.     {
  56.         return $this->description;
  57.     }
  58.     public function setDescription(string $description): static
  59.     {
  60.         $this->description = $description;
  61.         return $this;
  62.     }
  63.     /**
  64.      * @return Collection<int, Activity>
  65.      */
  66.     public function getActivities(): Collection
  67.     {
  68.         return $this->activities;
  69.     }
  70.     public function addActivity(Activity $activity): static
  71.     {
  72.         if (!$this->activities->contains($activity)) {
  73.             $this->activities->add($activity);
  74.             $activity->setUnite($this);
  75.         }
  76.         return $this;
  77.     }
  78.     public function removeActivity(Activity $activity): static
  79.     {
  80.         if ($this->activities->removeElement($activity)) {
  81.             // set the owning side to null (unless already changed)
  82.             if ($activity->getUnite() === $this) {
  83.                 $activity->setUnite(null);
  84.             }
  85.         }
  86.         return $this;
  87.     }
  88.     public function __toString() {
  89.         return $this->name;
  90.     }
  91. }