-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add tags to posts with many-to-many relation #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d77c8a7
c239d56
5dda271
2c91970
d787fb6
ec70fbd
813543f
5047756
a8b55d0
e72230f
169c42e
5226689
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <[email protected]> | ||
| * @author Javier Eguiluz <[email protected]> | ||
| * @author Rasanga Perera <[email protected]> | ||
| */ | ||
| 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(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <[email protected]> | ||
| * @author Javier Eguiluz <[email protected]> | ||
| * @author Rasanga Perera <[email protected]> | ||
| */ | ||
| class Post | ||
| { | ||
|
|
@@ -81,10 +83,21 @@ class Post | |
| */ | ||
| private $comments; | ||
|
|
||
| /** | ||
| * @var Collection|Tag[] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't use the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xabbuh, but collections support many useful methods:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but you then tie code using the entities to your internal implementation and I don't think we should encourage that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can tell the default is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, what's the verdict? I suggest that we should keep the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Rasanga yes, let's keep them as is |
||
| * | ||
| * @ORM\ManyToMany( | ||
| * targetEntity="Tag" | ||
| * ) | ||
| * @ORM\JoinTable(name="posts_tags") | ||
| */ | ||
| private $tags; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to initialize the collection in the constructor |
||
|
|
||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it correct condition?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @versh23, no. It should be just |
||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param Tag $tag | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function hasTag(Tag $tag) | ||
| { | ||
| return $this->tags->contains($tag); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| namespace AppBundle\Entity; | ||
|
|
||
| use Doctrine\ORM\Mapping as ORM; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
|
|
||
| /** | ||
| * Tag | ||
| * | ||
| * @ORM\Entity(repositoryClass="AppBundle\Repository\TagRepository") | ||
| * | ||
| * Defines the properties of the Tag entity to represent the blog post tag. | ||
| * See http://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class | ||
| * | ||
| * @author Rasanga Perera <[email protected]> | ||
| */ | ||
| 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; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about updating the repository to load with tags? To avoid lazy loading
And btw, inform what is lazy loading :)