src/Entity/Post.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Entity;
  11. use App\Repository\PostRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * Defines the properties of the Post entity to represent the blog posts.
  19.  *
  20.  * See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
  21.  *
  22.  * Tip: if you have an existing database, you can generate these entity class automatically.
  23.  * See https://symfony.com/doc/current/doctrine/reverse_engineering.html
  24.  *
  25.  * @author Ryan Weaver <weaverryan@gmail.com>
  26.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  27.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  28.  */
  29. #[ORM\Entity(repositoryClassPostRepository::class)]
  30. #[ORM\Table(name'symfony_demo_post')]
  31. #[UniqueEntity(fields: ['slug'], errorPath'title'message'post.slug_unique')]
  32. class Post
  33. {
  34.     #[ORM\Id]
  35.     #[ORM\GeneratedValue]
  36.     #[ORM\Column(type'integer')]
  37.     private ?int $id null;
  38.     #[ORM\Column(type'string')]
  39.     #[Assert\NotBlank]
  40.     private ?string $title null;
  41.     #[ORM\Column(type'string')]
  42.     private ?string $slug null;
  43.     #[ORM\Column(type'string')]
  44.     #[Assert\NotBlank(message'post.blank_summary')]
  45.     #[Assert\Length(max255)]
  46.     private ?string $summary null;
  47.     #[ORM\Column(type'text')]
  48.     #[Assert\NotBlank(message'post.blank_content')]
  49.     #[Assert\Length(min10minMessage'post.too_short_content')]
  50.     private ?string $content null;
  51.     #[ORM\Column(type'datetime')]
  52.     private \DateTime $publishedAt;
  53.     #[ORM\ManyToOne(targetEntityUser::class)]
  54.     #[ORM\JoinColumn(nullablefalse)]
  55.     private ?User $author null;
  56.     /**
  57.      * @var Comment[]|Collection
  58.      */
  59.     #[ORM\OneToMany(targetEntityComment::class, mappedBy'post'orphanRemovaltruecascade: ['persist'])]
  60.     #[ORM\OrderBy(['publishedAt' => 'DESC'])]
  61.     private Collection $comments;
  62.     /**
  63.      * @var Tag[]|Collection
  64.      */
  65.     #[ORM\ManyToMany(targetEntityTag::class, cascade: ['persist'])]
  66.     #[ORM\JoinTable(name'symfony_demo_post_tag')]
  67.     #[ORM\OrderBy(['name' => 'ASC'])]
  68.     #[Assert\Count(max4maxMessage'post.too_many_tags')]
  69.     private Collection $tags;
  70.     public function __construct()
  71.     {
  72.         $this->publishedAt = new \DateTime();
  73.         $this->comments = new ArrayCollection();
  74.         $this->tags = new ArrayCollection();
  75.     }
  76.     public function getId(): ?int
  77.     {
  78.         return $this->id;
  79.     }
  80.     public function getTitle(): ?string
  81.     {
  82.         return $this->title;
  83.     }
  84.     public function setTitle(?string $title): void
  85.     {
  86.         $this->title $title;
  87.     }
  88.     public function getSlug(): ?string
  89.     {
  90.         return $this->slug;
  91.     }
  92.     public function setSlug(string $slug): void
  93.     {
  94.         $this->slug $slug;
  95.     }
  96.     public function getContent(): ?string
  97.     {
  98.         return $this->content;
  99.     }
  100.     public function setContent(?string $content): void
  101.     {
  102.         $this->content $content;
  103.     }
  104.     public function getPublishedAt(): \DateTime
  105.     {
  106.         return $this->publishedAt;
  107.     }
  108.     public function setPublishedAt(\DateTime $publishedAt): void
  109.     {
  110.         $this->publishedAt $publishedAt;
  111.     }
  112.     public function getAuthor(): ?User
  113.     {
  114.         return $this->author;
  115.     }
  116.     public function setAuthor(User $author): void
  117.     {
  118.         $this->author $author;
  119.     }
  120.     public function getComments(): Collection
  121.     {
  122.         return $this->comments;
  123.     }
  124.     public function addComment(Comment $comment): void
  125.     {
  126.         $comment->setPost($this);
  127.         if (!$this->comments->contains($comment)) {
  128.             $this->comments->add($comment);
  129.         }
  130.     }
  131.     public function removeComment(Comment $comment): void
  132.     {
  133.         $this->comments->removeElement($comment);
  134.     }
  135.     public function getSummary(): ?string
  136.     {
  137.         return $this->summary;
  138.     }
  139.     public function setSummary(?string $summary): void
  140.     {
  141.         $this->summary $summary;
  142.     }
  143.     public function addTag(Tag ...$tags): void
  144.     {
  145.         foreach ($tags as $tag) {
  146.             if (!$this->tags->contains($tag)) {
  147.                 $this->tags->add($tag);
  148.             }
  149.         }
  150.     }
  151.     public function removeTag(Tag $tag): void
  152.     {
  153.         $this->tags->removeElement($tag);
  154.     }
  155.     public function getTags(): Collection
  156.     {
  157.         return $this->tags;
  158.     }
  159. }