src/Entity/CustomerDirectory.php line 21

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\Collection,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Serializer\Annotation\Groups,
  8.     Symfony\Component\Validator\Constraints as Assert,
  9.     Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use App\Entity\Trait\IdTrait,
  11.     App\Entity\Trait\UuidTrait,
  12.     App\Entity\Trait\TimestampableTrait,
  13.     App\Repository\CustomerDirectoryRepository;
  14. #[UniqueEntity(fields: ['parent''title'], errorPath'title'ignoreNullfalsemessage'This value already exists.')]
  15. #[ORM\UniqueConstraint(fields: ['parent''title'])]
  16. #[ORM\Entity(repositoryClassCustomerDirectoryRepository::class)]
  17. class CustomerDirectory
  18. {
  19.     use IdTrait,
  20.         UuidTrait,
  21.         TimestampableTrait;
  22.     #[Assert\NotNull]
  23.     #[ORM\ManyToOne(targetEntityCustomer::class, inversedBy'directories')]
  24.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  25.     private Customer $parent;
  26.     #[Assert\NotBlank]
  27.     #[Groups(['read'])]
  28.     #[ORM\Column(typeTypes::STRINGlength511)]
  29.     private ?string $title null;
  30.     #[ORM\OneToMany(targetEntityCustomerDirectoryItem::class, mappedBy'parent'cascade: ['persist''remove'])]
  31.     #[ORM\OrderBy(['createdAt' => 'desc'])]
  32.     private Collection $items;
  33.     public function __construct(Customer $parent)
  34.     {
  35.         $this->setUuid();
  36.         $this->parent $parent;
  37.         $this->items = new ArrayCollection();
  38.         $this->createdAt = new \DateTimeImmutable();
  39.         $this->updatedAt = new \DateTimeImmutable();
  40.         $parent->addDirectory($this);
  41.     }
  42.     public function getParent(): Customer
  43.     {
  44.         return $this->parent;
  45.     }
  46.     public function getTitle(): ?string
  47.     {
  48.         return $this->title;
  49.     }
  50.     public function setTitle(string $title): self
  51.     {
  52.         $this->title $title;
  53.         return $this;
  54.     }
  55.     /**
  56.      * @return Collection|Customer[]
  57.      */
  58.     public function getItems(): Collection
  59.     {
  60.         return $this->items;
  61.     }
  62.     public function addItem(CustomerDirectoryItem $item): self
  63.     {
  64.         if ($this->items->contains($item)) {
  65.             return $this;
  66.         }
  67.         $this->items[] = $item;
  68.         return $this;
  69.     }
  70.     public function removeItem(CustomerDirectoryItem $item): self
  71.     {
  72.         $this->items->removeElement($item);
  73.         return $this;
  74.     }
  75. }