src/Entity/BlogAuthor.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BlogAuthorRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. #[ORM\Entity(repositoryClassBlogAuthorRepository::class)]
  10. #[ORM\Table(name'blog_author')]
  11. #[ORM\HasLifecycleCallbacks]
  12. #[UniqueEntity(fields: ['email'], message'There is already an author with this email')]
  13. class BlogAuthor
  14. {
  15.     #[ORM\Id]
  16.     #[ORM\GeneratedValue]
  17.     #[ORM\Column]
  18.     private ?int $id null;
  19.     #[ORM\Column(length255)]
  20.     #[Assert\NotBlank]
  21.     private ?string $name null;
  22.     #[ORM\Column(type'text'nullabletrue)]
  23.     private ?string $bio null;
  24.     #[ORM\Column(length255nullabletrue)]
  25.     private ?string $avatar null;
  26.     #[ORM\Column(length255uniquetrue)]
  27.     #[Assert\NotBlank]
  28.     #[Assert\Email]
  29.     private ?string $email null;
  30.     #[ORM\Column]
  31.     private ?\DateTimeImmutable $createdAt null;
  32.     #[ORM\OneToMany(mappedBy'author'targetEntityBlog::class)]
  33.     private Collection $blogPosts;
  34.     public function __construct()
  35.     {
  36.         $this->blogPosts = new ArrayCollection();
  37.         $this->createdAt = new \DateTimeImmutable();
  38.     }
  39.     public function getId(): ?int
  40.     {
  41.         return $this->id;
  42.     }
  43.     public function getName(): ?string
  44.     {
  45.         return $this->name;
  46.     }
  47.     public function setName(string $name): static
  48.     {
  49.         $this->name $name;
  50.         return $this;
  51.     }
  52.     public function getBio(): ?string
  53.     {
  54.         return $this->bio;
  55.     }
  56.     public function setBio(?string $bio): static
  57.     {
  58.         $this->bio $bio;
  59.         return $this;
  60.     }
  61.     public function getAvatar(): ?string
  62.     {
  63.         return $this->avatar;
  64.     }
  65.     public function setAvatar(?string $avatar): static
  66.     {
  67.         $this->avatar $avatar;
  68.         return $this;
  69.     }
  70.     public function getEmail(): ?string
  71.     {
  72.         return $this->email;
  73.     }
  74.     public function setEmail(string $email): static
  75.     {
  76.         $this->email $email;
  77.         return $this;
  78.     }
  79.     public function getCreatedAt(): ?\DateTimeImmutable
  80.     {
  81.         return $this->createdAt;
  82.     }
  83.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  84.     {
  85.         $this->createdAt $createdAt;
  86.         return $this;
  87.     }
  88.     /**
  89.      * @return Collection<int, Blog>
  90.      */
  91.     public function getBlogPosts(): Collection
  92.     {
  93.         return $this->blogPosts;
  94.     }
  95.     public function addBlogPost(Blog $blogPost): static
  96.     {
  97.         if (!$this->blogPosts->contains($blogPost)) {
  98.             $this->blogPosts->add($blogPost);
  99.             $blogPost->setAuthor($this);
  100.         }
  101.         return $this;
  102.     }
  103.     public function removeBlogPost(Blog $blogPost): static
  104.     {
  105.         if ($this->blogPosts->removeElement($blogPost)) {
  106.             // set the owning side to null (unless already changed)
  107.             if ($blogPost->getAuthor() === $this) {
  108.                 $blogPost->setAuthor(null);
  109.             }
  110.         }
  111.         return $this;
  112.     }
  113.     public function __toString(): string
  114.     {
  115.         return $this->name ?? 'New Author';
  116.     }
  117. }