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 Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. /**
  17.  * @ORM\Entity(repositoryClass="App\Repository\PostRepository")
  18.  * @ORM\Table(name="symfony_demo_post")
  19.  * @UniqueEntity(fields={"slug"}, errorPath="title", message="post.slug_unique")
  20.  *
  21.  * Defines the properties of the Post entity to represent the blog posts.
  22.  *
  23.  * See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
  24.  *
  25.  * Tip: if you have an existing database, you can generate these entity class automatically.
  26.  * See https://symfony.com/doc/current/doctrine/reverse_engineering.html
  27.  *
  28.  * @author Ryan Weaver <weaverryan@gmail.com>
  29.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  30.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  31.  */
  32. class Post
  33. {
  34.     /**
  35.      * @var int
  36.      *
  37.      * @ORM\Id
  38.      * @ORM\GeneratedValue
  39.      * @ORM\Column(type="integer")
  40.      */
  41.     private $id;
  42.     /**
  43.      * @var string
  44.      *
  45.      * @ORM\Column(type="string")
  46.      * @Assert\NotBlank
  47.      */
  48.     private $title;
  49.     /**
  50.      * @var string
  51.      *
  52.      * @ORM\Column(type="string")
  53.      */
  54.     private $slug;
  55.     /**
  56.      * @var string
  57.      *
  58.      * @ORM\Column(type="string")
  59.      * @Assert\NotBlank(message="post.blank_summary")
  60.      * @Assert\Length(max=255)
  61.      */
  62.     private $summary;
  63.     /**
  64.      * @var string
  65.      *
  66.      * @ORM\Column(type="text")
  67.      * @Assert\NotBlank(message="post.blank_content")
  68.      * @Assert\Length(min=10, minMessage="post.too_short_content")
  69.      */
  70.     private $content;
  71.     /**
  72.      * @var \DateTime
  73.      *
  74.      * @ORM\Column(type="datetime")
  75.      */
  76.     private $publishedAt;
  77.     /**
  78.      * @var User
  79.      *
  80.      * @ORM\ManyToOne(targetEntity="App\Entity\User")
  81.      * @ORM\JoinColumn(nullable=false)
  82.      */
  83.     private $author;
  84.     /**
  85.      * @var Comment[]|Collection
  86.      *
  87.      * @ORM\OneToMany(
  88.      *      targetEntity="Comment",
  89.      *      mappedBy="post",
  90.      *      orphanRemoval=true,
  91.      *      cascade={"persist"}
  92.      * )
  93.      * @ORM\OrderBy({"publishedAt": "DESC"})
  94.      */
  95.     private $comments;
  96.     /**
  97.      * @var Tag[]|Collection
  98.      *
  99.      * @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"})
  100.      * @ORM\JoinTable(name="symfony_demo_post_tag")
  101.      * @ORM\OrderBy({"name": "ASC"})
  102.      * @Assert\Count(max="4", maxMessage="post.too_many_tags")
  103.      */
  104.     private $tags;
  105.     public function __construct()
  106.     {
  107.         $this->publishedAt = new \DateTime();
  108.         $this->comments = new ArrayCollection();
  109.         $this->tags = new ArrayCollection();
  110.     }
  111.     public function getId(): ?int
  112.     {
  113.         return $this->id;
  114.     }
  115.     public function getTitle(): ?string
  116.     {
  117.         return $this->title;
  118.     }
  119.     public function setTitle(?string $title): void
  120.     {
  121.         $this->title $title;
  122.     }
  123.     public function getSlug(): ?string
  124.     {
  125.         return $this->slug;
  126.     }
  127.     public function setSlug(string $slug): void
  128.     {
  129.         $this->slug $slug;
  130.     }
  131.     public function getContent(): ?string
  132.     {
  133.         return $this->content;
  134.     }
  135.     public function setContent(?string $content): void
  136.     {
  137.         $this->content $content;
  138.     }
  139.     public function getPublishedAt(): \DateTime
  140.     {
  141.         return $this->publishedAt;
  142.     }
  143.     public function setPublishedAt(\DateTime $publishedAt): void
  144.     {
  145.         $this->publishedAt $publishedAt;
  146.     }
  147.     public function getAuthor(): ?User
  148.     {
  149.         return $this->author;
  150.     }
  151.     public function setAuthor(User $author): void
  152.     {
  153.         $this->author $author;
  154.     }
  155.     public function getComments(): Collection
  156.     {
  157.         return $this->comments;
  158.     }
  159.     public function addComment(Comment $comment): void
  160.     {
  161.         $comment->setPost($this);
  162.         if (!$this->comments->contains($comment)) {
  163.             $this->comments->add($comment);
  164.         }
  165.     }
  166.     public function removeComment(Comment $comment): void
  167.     {
  168.         $this->comments->removeElement($comment);
  169.     }
  170.     public function getSummary(): ?string
  171.     {
  172.         return $this->summary;
  173.     }
  174.     public function setSummary(?string $summary): void
  175.     {
  176.         $this->summary $summary;
  177.     }
  178.     public function addTag(Tag ...$tags): void
  179.     {
  180.         foreach ($tags as $tag) {
  181.             if (!$this->tags->contains($tag)) {
  182.                 $this->tags->add($tag);
  183.             }
  184.         }
  185.     }
  186.     public function removeTag(Tag $tag): void
  187.     {
  188.         $this->tags->removeElement($tag);
  189.     }
  190.     public function getTags(): Collection
  191.     {
  192.         return $this->tags;
  193.     }
  194. }