From 46bbd8d321e6a00131f0e6ed96fa6f3d693b3678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 24 Feb 2015 07:42:09 +0100 Subject: relation between tags and entries --- .../CoreBundle/DataFixtures/ORM/LoadEntryData.php | 9 +++ .../CoreBundle/DataFixtures/ORM/LoadTagData.php | 61 ---------------- src/Wallabag/CoreBundle/Entity/Entry.php | 8 +-- src/Wallabag/CoreBundle/Entity/Tag.php | 12 +++- src/Wallabag/CoreBundle/Entity/TagsEntries.php | 81 ---------------------- .../CoreBundle/Repository/EntryRepository.php | 11 +++ .../Controller/WallabagRestControllerTest.php | 26 +++++++ 7 files changed, 60 insertions(+), 148 deletions(-) delete mode 100644 src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php delete mode 100644 src/Wallabag/CoreBundle/Entity/TagsEntries.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php index 3be323ed..edab9adc 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php @@ -6,6 +6,7 @@ use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\CoreBundle\Entity\Tag; class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface { @@ -37,6 +38,14 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry3->setTitle('test title entry3'); $entry3->setContent('This is my content /o/'); + $tag1 = new Tag(); + $tag1->setLabel("foo"); + $tag2 = new Tag(); + $tag2->setLabel("bar"); + + $entry3->addTag($tag1); + $entry3->addTag($tag2); + $manager->persist($entry3); $this->addReference('entry3', $entry3); diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php deleted file mode 100644 index 4d9846b6..00000000 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php +++ /dev/null @@ -1,61 +0,0 @@ -setLabel('foo'); - - $manager->persist($tag1); - - $this->addReference('tag1', $tag1); - - $tagsEntries1 = new TagsEntries(); - $tagsEntries1->setEntryId($this->getReference('entry1')); - $manager->persist($tagsEntries1); - - $tag2 = new Tag(); - $tag2->setLabel('bar'); - - $manager->persist($tag2); - - $this->addReference('tag2', $tag2); - - $tagsEntries2 = new TagsEntries(); - $tagsEntries2->setEntryId($this->getReference('entry2')); - $manager->persist($tagsEntries2); - - $tag3 = new Tag(); - $tag3->setLabel('baz'); - - $manager->persist($tag3); - - $this->addReference('tag3', $tag3); - - $tagsEntries3 = new TagsEntries(); - $tagsEntries3->setEntryId($this->getReference('entry2')); - $manager->persist($tagsEntries3); - - $manager->flush(); - } - - /** - * {@inheritDoc} - */ - public function getOrder() - { - return 30; - } -} diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index e0d1b839..229a6704 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -120,11 +120,8 @@ class Entry private $user; /** - * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist", "merge"}) - * @ORM\JoinTable(name="tags_entries", - * joinColumns={@ORM\JoinColumn(name="entry_id", referencedColumnName="id")}, - * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")} - * ) + * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) + * @ORM\JoinTable(name="entry_tags") */ private $tags; @@ -407,5 +404,6 @@ class Entry public function addTag(Tag $tag) { $this->tags[] = $tag; + $tag->addEntry($this); } } diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 1cdf4df0..5aed1fa0 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -6,6 +6,7 @@ use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation\XmlRoot; use JMS\Serializer\Annotation\ExclusionPolicy; use JMS\Serializer\Annotation\Expose; +use Doctrine\Common\Collections\ArrayCollection; /** * Tag @@ -36,10 +37,14 @@ class Tag private $label; /** - * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist", "merge"}) + * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"}) */ private $entries; + public function __construct() + { + $this->entries = new ArrayCollection(); + } /** * Get id * @@ -72,4 +77,9 @@ class Tag { return $this->label; } + + public function addEntry(Entry $entry) + { + $this->entries[] = $entry; + } } diff --git a/src/Wallabag/CoreBundle/Entity/TagsEntries.php b/src/Wallabag/CoreBundle/Entity/TagsEntries.php deleted file mode 100644 index 589f26f8..00000000 --- a/src/Wallabag/CoreBundle/Entity/TagsEntries.php +++ /dev/null @@ -1,81 +0,0 @@ -id; - } - - /** - * @return mixed - */ - public function getEntryId() - { - return $this->entryId; - } - - /** - * @param mixed $entryId - */ - public function setEntryId($entryId) - { - $this->entryId = $entryId; - } - - /** - * @return mixed - */ - public function getTagId() - { - return $this->tagId; - } - - /** - * @param mixed $tagId - */ - public function setTagId($tagId) - { - $this->tagId = $tagId; - } - -} diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 005142fc..f6f60c6f 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -118,4 +118,15 @@ class EntryRepository extends EntityRepository ->getQuery() ->getResult(); } + + public function findOneWithTags() + { + $qb = $this->createQueryBuilder('e') + ->innerJoin('e.tags', 't') + ->addSelect('t'); + + return $qb + ->getQuery() + ->getOneOrNullResult(); + } } diff --git a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php index 4164e516..d2390055 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php @@ -150,4 +150,30 @@ class WallabagRestControllerTest extends WallabagTestCase $this->assertEquals(404, $client->getResponse()->getStatusCode()); } + + public function testGetTagsEntry() + { + $client = $this->createClient(); + $client->request('GET', '/api/salts/admin.json'); + $salt = json_decode($client->getResponse()->getContent()); + $headers = $this->generateHeaders('admin', 'test', $salt[0]); + + $entry = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneWithTags(); + + if (!$entry) { + $this->markTestSkipped('No content found in db.'); + } + + $tags = array(); + foreach ($entry->getTags() as $tag) { + $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel()); + } + + $client->request('GET', '/api/entries/'.$entry->getId().'/tags', array(), array(), $headers); + + $this->assertEquals(json_encode($tags), $client->getResponse()->getContent()); + } } -- cgit v1.2.3