Advertisement
Guest User

Post Enity

a guest
Oct 6th, 2017
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.73 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * This file is part of the Symfony package.
  5.  *
  6.  * (c) Fabien Potencier <fabien@symfony.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11.  
  12. namespace AppBundle\Entity;
  13.  
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17.  
  18. /**
  19.  * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
  20.  * @ORM\Table(name="symfony_demo_post")
  21.  *
  22.  * Defines the properties of the Post entity to represent the blog posts.
  23.  *
  24.  * See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class
  25.  *
  26.  * Tip: if you have an existing database, you can generate these entity class automatically.
  27.  * See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
  28.  *
  29.  * @author Ryan Weaver <weaverryan@gmail.com>
  30.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  31.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  32.  */
  33. class Post
  34. {
  35.     /**
  36.      * Use constants to define configuration options that rarely change instead
  37.      * of specifying them in app/config/config.yml.
  38.      *
  39.      * See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options
  40.      */
  41.     const NUM_ITEMS = 10;
  42.  
  43.     /**
  44.      * @var int
  45.      *
  46.      * @ORM\Id
  47.      * @ORM\GeneratedValue
  48.      * @ORM\Column(type="integer")
  49.      */
  50.     private $id;
  51.  
  52.     /**
  53.      * @var string
  54.      *
  55.      * @ORM\Column(type="string")
  56.      * @Assert\NotBlank
  57.      */
  58.     private $title;
  59.  
  60.     /**
  61.      * @var string
  62.      *
  63.      * @ORM\Column(type="string")
  64.      */
  65.     private $slug;
  66.  
  67.     /**
  68.      * @var string
  69.      *
  70.      * @ORM\Column(type="string")
  71.      * @Assert\NotBlank(message="post.blank_summary")
  72.      */
  73.     private $summary;
  74.  
  75.     /**
  76.      * @var string
  77.      *
  78.      * @ORM\Column(type="text")
  79.      * @Assert\NotBlank(message="post.blank_content")
  80.      * @Assert\Length(min=10, minMessage="post.too_short_content")
  81.      */
  82.     private $content;
  83.  
  84.     /**
  85.      * @var \DateTime
  86.      *
  87.      * @ORM\Column(type="datetime")
  88.      * @Assert\DateTime
  89.      */
  90.     private $publishedAt;
  91.  
  92.     /**
  93.      * @var User
  94.      *
  95.      * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
  96.      * @ORM\JoinColumn(nullable=false)
  97.      */
  98.     private $author;
  99.  
  100.     /**
  101.      * @var Comment[]|ArrayCollection
  102.      *
  103.      * @ORM\OneToMany(
  104.      *      targetEntity="Comment",
  105.      *      mappedBy="post",
  106.      *      orphanRemoval=true
  107.      * )
  108.      * @ORM\OrderBy({"publishedAt": "DESC"})
  109.      */
  110.     private $comments;
  111.  
  112.     /**
  113.      * @var Tag[]|ArrayCollection
  114.      *
  115.      * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Tag", cascade={"persist"})
  116.      * @ORM\JoinTable(name="symfony_demo_post_tag")
  117.      * @ORM\OrderBy({"name": "ASC"})
  118.      * @Assert\Count(max="4", maxMessage="post.too_many_tags")
  119.      */
  120.     private $tags;
  121.  
  122.  
  123.     /**
  124.      * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
  125.      * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
  126.      */
  127.     private $category;
  128.  
  129.  
  130.     public function __construct()
  131.     {
  132.         $this->publishedAt = new \DateTime();
  133.         $this->comments = new ArrayCollection();
  134.         $this->tags = new ArrayCollection();
  135.     }
  136.  
  137.     public function getId()
  138.     {
  139.         return $this->id;
  140.     }
  141.  
  142.     public function getTitle()
  143.     {
  144.         return $this->title;
  145.     }
  146.  
  147.     /**
  148.      * @param string $title
  149.      */
  150.     public function setTitle($title)
  151.     {
  152.         $this->title = $title;
  153.     }
  154.  
  155.     public function getSlug()
  156.     {
  157.         return $this->slug;
  158.     }
  159.  
  160.     /**
  161.      * @param string $slug
  162.      */
  163.     public function setSlug($slug)
  164.     {
  165.         $this->slug = $slug;
  166.     }
  167.  
  168.     public function getContent()
  169.     {
  170.         return $this->content;
  171.     }
  172.  
  173.     /**
  174.      * @param string $content
  175.      */
  176.     public function setContent($content)
  177.     {
  178.         $this->content = $content;
  179.     }
  180.  
  181.     public function getPublishedAt()
  182.     {
  183.         return $this->publishedAt;
  184.     }
  185.  
  186.     public function setPublishedAt(\DateTime $publishedAt)
  187.     {
  188.         $this->publishedAt = $publishedAt;
  189.     }
  190.  
  191.     /**
  192.      * @return User
  193.      */
  194.     public function getAuthor()
  195.     {
  196.         return $this->author;
  197.     }
  198.  
  199.     /**
  200.      * @param User $author
  201.      */
  202.     public function setAuthor(User $author)
  203.     {
  204.         $this->author = $author;
  205.     }
  206.  
  207.     public function getComments()
  208.     {
  209.         return $this->comments;
  210.     }
  211.  
  212.     public function addComment(Comment $comment)
  213.     {
  214.         $comment->setPost($this);
  215.         if (!$this->comments->contains($comment)) {
  216.             $this->comments->add($comment);
  217.         }
  218.     }
  219.  
  220.     public function removeComment(Comment $comment)
  221.     {
  222.         $comment->setPost(null);
  223.         $this->comments->removeElement($comment);
  224.     }
  225.  
  226.     public function getSummary()
  227.     {
  228.         return $this->summary;
  229.     }
  230.  
  231.     /**
  232.      * @param string $summary
  233.      */
  234.     public function setSummary($summary)
  235.     {
  236.         $this->summary = $summary;
  237.     }
  238.  
  239.     public function addTag(Tag $tag)
  240.     {
  241.         if (!$this->tags->contains($tag)) {
  242.             $this->tags->add($tag);
  243.         }
  244.     }
  245.  
  246.     public function removeTag(Tag $tag)
  247.     {
  248.         $this->tags->removeElement($tag);
  249.     }
  250.  
  251.     public function getTags()
  252.     {
  253.         return $this->tags;
  254.     }
  255.  
  256.     /**
  257.      * @return Category
  258.      */
  259.     public function getCategory()
  260.     {
  261.         return $this->category;
  262.     }
  263.  
  264.     /**
  265.      * @param Category $category
  266.      */
  267.     public function setCategory(Category $category)
  268.     {
  269.         $this->category = $category;
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement