diff --git a/app/Resources/views/blog/index.html.twig b/app/Resources/views/blog/index.html.twig index 09194abc5..f66fd5973 100644 --- a/app/Resources/views/blog/index.html.twig +++ b/app/Resources/views/blog/index.html.twig @@ -10,6 +10,13 @@ {{ post.title }} + {% if 0 < post.tags | length %} +

+ {% for tag in post.tags %} + {{ tag.name }} + {% endfor %} +

+ {% endif %} {{ post.summary|md2html }} diff --git a/app/Resources/views/blog/post_show.html.twig b/app/Resources/views/blog/post_show.html.twig index c574ad115..aea0aa0c1 100644 --- a/app/Resources/views/blog/post_show.html.twig +++ b/app/Resources/views/blog/post_show.html.twig @@ -5,6 +5,14 @@ {% block main %}

{{ post.title }}

+ {% if 0 < post.tags | length %} +

Tags: + {% for tag in post.tags %} + {{ tag.name }} + {% endfor %} +

+ {% endif %} + {{ post.content|md2html }}
diff --git a/src/AppBundle/DataFixtures/ORM/LoadFixtures.php b/src/AppBundle/DataFixtures/ORM/LoadFixtures.php index 901e859dd..ac82d53b5 100644 --- a/src/AppBundle/DataFixtures/ORM/LoadFixtures.php +++ b/src/AppBundle/DataFixtures/ORM/LoadFixtures.php @@ -11,9 +11,11 @@ namespace AppBundle\DataFixtures\ORM; +use AppBundle\Entity\Tag; use AppBundle\Entity\User; use AppBundle\Entity\Post; use AppBundle\Entity\Comment; +use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\DependencyInjection\ContainerAwareInterface; @@ -29,6 +31,7 @@ * * @author Ryan Weaver * @author Javier Eguiluz + * @author Rasanga Perera */ class LoadFixtures implements FixtureInterface, ContainerAwareInterface { @@ -41,7 +44,11 @@ class LoadFixtures implements FixtureInterface, ContainerAwareInterface public function load(ObjectManager $manager) { $this->loadUsers($manager); - $this->loadPosts($manager); + $this->loadPostsTags( + $manager, + $this->loadPosts($manager), + $this->loadTags($manager) + ); } private function loadUsers(ObjectManager $manager) @@ -66,8 +73,15 @@ private function loadUsers(ObjectManager $manager) $manager->flush(); } + /** + * @param ObjectManager $manager + * + * @return array + */ private function loadPosts(ObjectManager $manager) { + $posts = array(); + foreach (range(1, 30) as $i) { $post = new Post(); @@ -91,9 +105,94 @@ private function loadPosts(ObjectManager $manager) } $manager->persist($post); + + $posts[] = $post; + } + + $manager->flush(); + + return $posts; + } + + /** + * @param ObjectManager $manager + * + * @return array + */ + private function loadTags(ObjectManager $manager) + { + $tags = array(); + $tagNames = array( + 'Lorem', + 'ipsum', + 'consectetur', + 'adipiscing', + 'incididunt', + 'labore', + 'voluptate' + ); + + foreach ($tagNames as $key => $name) { + $tag = new Tag(); + $tag->setName($name); + + $manager->persist($tag); + + $tags[] = $tag; } $manager->flush(); + + return $tags; + } + + /** + * @param ObjectManager $manager + * @param array|Post[] $posts + * @param array|Tag[] $tags + */ + private function loadPostsTags( + ObjectManager $manager, + array $posts, + array $tags + ) { + $posts[0]->addTag($tags[0]); + $posts[0]->addTag($tags[2]); + $manager->persist($posts[0]); + + $posts[1]->addTag($tags[1]); + $manager->persist($posts[1]); + + $posts[2]->addTag($tags[2]); + $posts[2]->addTag($tags[3]); + $posts[2]->addTag($tags[4]); + $manager->persist($posts[2]); + + $posts[3]->addTag($tags[5]); + $posts[3]->addTag($tags[6]); + $manager->persist($posts[3]); + + $posts[4]->addTag($tags[0]); + $manager->persist($posts[4]); + + $posts[5]->addTag($tags[0]); + $manager->persist($posts[5]); + + $posts[6]->addTag($tags[2]); + $manager->persist($posts[6]); + + $posts[7]->addTag($tags[4]); + $posts[7]->addTag($tags[6]); + $manager->persist($posts[7]); + + $posts[8]->addTag($tags[0]); + $manager->persist($posts[8]); + + $posts[9]->addTag($tags[3]); + $posts[9]->addTag($tags[5]); + $manager->persist($posts[9]); + + $manager->flush(); } /** diff --git a/src/AppBundle/Entity/Post.php b/src/AppBundle/Entity/Post.php index 63f1b07ce..5ab5759af 100644 --- a/src/AppBundle/Entity/Post.php +++ b/src/AppBundle/Entity/Post.php @@ -2,6 +2,7 @@ namespace AppBundle\Entity; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Validator\Constraints as Assert; @@ -18,6 +19,7 @@ * * @author Ryan Weaver * @author Javier Eguiluz + * @author Rasanga Perera */ class Post { @@ -81,10 +83,21 @@ class Post */ private $comments; + /** + * @var Collection|Tag[] + * + * @ORM\ManyToMany( + * targetEntity="Tag" + * ) + * @ORM\JoinTable(name="posts_tags") + */ + private $tags; + public function __construct() { $this->publishedAt = new \DateTime(); $this->comments = new ArrayCollection(); + $this->tags = new ArrayCollection(); } public function getId() @@ -179,4 +192,56 @@ public function setSummary($summary) { $this->summary = $summary; } + + /** + * Get tags + * + * @return Tag[]|Collection + */ + public function getTags() + { + return $this->tags; + } + + /** + * Add tag + * + * @param Tag $tag + * + * @return Post + */ + public function addTag(Tag $tag) + { + if (!$this->hasTag($tag)) { + $this->tags->add($tag); + } + + return $this; + } + + /** + * Remove tag + * + * @param Tag $tag + * + * @return Post + */ + public function removeTag(Tag $tag) + { + if (!$this->hasTag($tag)) { + $this->tags->removeElement($tag); + } + + return $this; + } + + /** + * @param Tag $tag + * + * @return bool + */ + public function hasTag(Tag $tag) + { + return $this->tags->contains($tag); + } } diff --git a/src/AppBundle/Entity/Tag.php b/src/AppBundle/Entity/Tag.php new file mode 100644 index 000000000..634dca43d --- /dev/null +++ b/src/AppBundle/Entity/Tag.php @@ -0,0 +1,70 @@ + + */ +class Tag +{ + /** + * @var integer + * + * @ORM\Column(name="id", type="integer") + * @ORM\Id + * @ORM\GeneratedValue(strategy="AUTO") + */ + private $id; + + /** + * @var string + * + * @ORM\Column(type="string", length=255) + * @Assert\NotBlank() + */ + private $name; + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set name + * + * @param string $name + * + * @return Tag + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Get name + * + * @return string + */ + public function getName() + { + return $this->name; + } +}