diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-02-24 07:42:09 +0100 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-03-06 20:50:31 +0100 |
commit | 46bbd8d321e6a00131f0e6ed96fa6f3d693b3678 (patch) | |
tree | 331d511837716ec7a7956c35f51dc3a14a1ca11c /src | |
parent | 6c87418ff013cfd03093c3f01e20518e580d80bb (diff) | |
download | wallabag-46bbd8d321e6a00131f0e6ed96fa6f3d693b3678.tar.gz wallabag-46bbd8d321e6a00131f0e6ed96fa6f3d693b3678.tar.zst wallabag-46bbd8d321e6a00131f0e6ed96fa6f3d693b3678.zip |
relation between tags and entries
Diffstat (limited to 'src')
7 files changed, 60 insertions, 148 deletions
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; | |||
6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | 6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
7 | use Doctrine\Common\Persistence\ObjectManager; | 7 | use Doctrine\Common\Persistence\ObjectManager; |
8 | use Wallabag\CoreBundle\Entity\Entry; | 8 | use Wallabag\CoreBundle\Entity\Entry; |
9 | use Wallabag\CoreBundle\Entity\Tag; | ||
9 | 10 | ||
10 | class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface | 11 | class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface |
11 | { | 12 | { |
@@ -37,6 +38,14 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface | |||
37 | $entry3->setTitle('test title entry3'); | 38 | $entry3->setTitle('test title entry3'); |
38 | $entry3->setContent('This is my content /o/'); | 39 | $entry3->setContent('This is my content /o/'); |
39 | 40 | ||
41 | $tag1 = new Tag(); | ||
42 | $tag1->setLabel("foo"); | ||
43 | $tag2 = new Tag(); | ||
44 | $tag2->setLabel("bar"); | ||
45 | |||
46 | $entry3->addTag($tag1); | ||
47 | $entry3->addTag($tag2); | ||
48 | |||
40 | $manager->persist($entry3); | 49 | $manager->persist($entry3); |
41 | 50 | ||
42 | $this->addReference('entry3', $entry3); | 51 | $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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\DataFixtures\ORM; | ||
4 | |||
5 | use Doctrine\Common\DataFixtures\AbstractFixture; | ||
6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | ||
7 | use Doctrine\Common\Persistence\ObjectManager; | ||
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
9 | use Wallabag\CoreBundle\Entity\TagsEntries; | ||
10 | |||
11 | class LoadTagData extends AbstractFixture implements OrderedFixtureInterface | ||
12 | { | ||
13 | /** | ||
14 | * {@inheritDoc} | ||
15 | */ | ||
16 | public function load(ObjectManager $manager) | ||
17 | { | ||
18 | $tag1 = new Tag(); | ||
19 | $tag1->setLabel('foo'); | ||
20 | |||
21 | $manager->persist($tag1); | ||
22 | |||
23 | $this->addReference('tag1', $tag1); | ||
24 | |||
25 | $tagsEntries1 = new TagsEntries(); | ||
26 | $tagsEntries1->setEntryId($this->getReference('entry1')); | ||
27 | $manager->persist($tagsEntries1); | ||
28 | |||
29 | $tag2 = new Tag(); | ||
30 | $tag2->setLabel('bar'); | ||
31 | |||
32 | $manager->persist($tag2); | ||
33 | |||
34 | $this->addReference('tag2', $tag2); | ||
35 | |||
36 | $tagsEntries2 = new TagsEntries(); | ||
37 | $tagsEntries2->setEntryId($this->getReference('entry2')); | ||
38 | $manager->persist($tagsEntries2); | ||
39 | |||
40 | $tag3 = new Tag(); | ||
41 | $tag3->setLabel('baz'); | ||
42 | |||
43 | $manager->persist($tag3); | ||
44 | |||
45 | $this->addReference('tag3', $tag3); | ||
46 | |||
47 | $tagsEntries3 = new TagsEntries(); | ||
48 | $tagsEntries3->setEntryId($this->getReference('entry2')); | ||
49 | $manager->persist($tagsEntries3); | ||
50 | |||
51 | $manager->flush(); | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * {@inheritDoc} | ||
56 | */ | ||
57 | public function getOrder() | ||
58 | { | ||
59 | return 30; | ||
60 | } | ||
61 | } | ||
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 | |||
120 | private $user; | 120 | private $user; |
121 | 121 | ||
122 | /** | 122 | /** |
123 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist", "merge"}) | 123 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) |
124 | * @ORM\JoinTable(name="tags_entries", | 124 | * @ORM\JoinTable(name="entry_tags") |
125 | * joinColumns={@ORM\JoinColumn(name="entry_id", referencedColumnName="id")}, | ||
126 | * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")} | ||
127 | * ) | ||
128 | */ | 125 | */ |
129 | private $tags; | 126 | private $tags; |
130 | 127 | ||
@@ -407,5 +404,6 @@ class Entry | |||
407 | public function addTag(Tag $tag) | 404 | public function addTag(Tag $tag) |
408 | { | 405 | { |
409 | $this->tags[] = $tag; | 406 | $this->tags[] = $tag; |
407 | $tag->addEntry($this); | ||
410 | } | 408 | } |
411 | } | 409 | } |
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; | |||
6 | use JMS\Serializer\Annotation\XmlRoot; | 6 | use JMS\Serializer\Annotation\XmlRoot; |
7 | use JMS\Serializer\Annotation\ExclusionPolicy; | 7 | use JMS\Serializer\Annotation\ExclusionPolicy; |
8 | use JMS\Serializer\Annotation\Expose; | 8 | use JMS\Serializer\Annotation\Expose; |
9 | use Doctrine\Common\Collections\ArrayCollection; | ||
9 | 10 | ||
10 | /** | 11 | /** |
11 | * Tag | 12 | * Tag |
@@ -36,10 +37,14 @@ class Tag | |||
36 | private $label; | 37 | private $label; |
37 | 38 | ||
38 | /** | 39 | /** |
39 | * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist", "merge"}) | 40 | * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"}) |
40 | */ | 41 | */ |
41 | private $entries; | 42 | private $entries; |
42 | 43 | ||
44 | public function __construct() | ||
45 | { | ||
46 | $this->entries = new ArrayCollection(); | ||
47 | } | ||
43 | /** | 48 | /** |
44 | * Get id | 49 | * Get id |
45 | * | 50 | * |
@@ -72,4 +77,9 @@ class Tag | |||
72 | { | 77 | { |
73 | return $this->label; | 78 | return $this->label; |
74 | } | 79 | } |
80 | |||
81 | public function addEntry(Entry $entry) | ||
82 | { | ||
83 | $this->entries[] = $entry; | ||
84 | } | ||
75 | } | 85 | } |
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Entity; | ||
4 | |||
5 | use Doctrine\ORM\Mapping as ORM; | ||
6 | |||
7 | /** | ||
8 | * TagsEntries | ||
9 | * | ||
10 | * @ORM\Table(name="tags_entries") | ||
11 | */ | ||
12 | class TagsEntries | ||
13 | { | ||
14 | /** | ||
15 | * @var integer | ||
16 | * | ||
17 | * @ORM\Column(name="id", type="integer") | ||
18 | * @ORM\Id | ||
19 | * @ORM\GeneratedValue(strategy="AUTO") | ||
20 | */ | ||
21 | private $id; | ||
22 | |||
23 | /** | ||
24 | * | ||
25 | * @ORM\ManyToOne(targetEntity="Entry", inversedBy="tags_entries") | ||
26 | * @ORM\JoinColumn(name="entry_id", referencedColumnName="id") | ||
27 | * | ||
28 | */ | ||
29 | private $entryId; | ||
30 | |||
31 | /** | ||
32 | * | ||
33 | * @ORM\ManyToOne(targetEntity="Tag", inversedBy="tags_entries") | ||
34 | * @ORM\JoinColumn(name="tag_id", referencedColumnName="id") | ||
35 | * | ||
36 | */ | ||
37 | private $tagId; | ||
38 | |||
39 | /** | ||
40 | * Get id | ||
41 | * | ||
42 | * @return integer | ||
43 | */ | ||
44 | public function getId() | ||
45 | { | ||
46 | return $this->id; | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * @return mixed | ||
51 | */ | ||
52 | public function getEntryId() | ||
53 | { | ||
54 | return $this->entryId; | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * @param mixed $entryId | ||
59 | */ | ||
60 | public function setEntryId($entryId) | ||
61 | { | ||
62 | $this->entryId = $entryId; | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * @return mixed | ||
67 | */ | ||
68 | public function getTagId() | ||
69 | { | ||
70 | return $this->tagId; | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * @param mixed $tagId | ||
75 | */ | ||
76 | public function setTagId($tagId) | ||
77 | { | ||
78 | $this->tagId = $tagId; | ||
79 | } | ||
80 | |||
81 | } | ||
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 | |||
118 | ->getQuery() | 118 | ->getQuery() |
119 | ->getResult(); | 119 | ->getResult(); |
120 | } | 120 | } |
121 | |||
122 | public function findOneWithTags() | ||
123 | { | ||
124 | $qb = $this->createQueryBuilder('e') | ||
125 | ->innerJoin('e.tags', 't') | ||
126 | ->addSelect('t'); | ||
127 | |||
128 | return $qb | ||
129 | ->getQuery() | ||
130 | ->getOneOrNullResult(); | ||
131 | } | ||
121 | } | 132 | } |
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 | |||
150 | 150 | ||
151 | $this->assertEquals(404, $client->getResponse()->getStatusCode()); | 151 | $this->assertEquals(404, $client->getResponse()->getStatusCode()); |
152 | } | 152 | } |
153 | |||
154 | public function testGetTagsEntry() | ||
155 | { | ||
156 | $client = $this->createClient(); | ||
157 | $client->request('GET', '/api/salts/admin.json'); | ||
158 | $salt = json_decode($client->getResponse()->getContent()); | ||
159 | $headers = $this->generateHeaders('admin', 'test', $salt[0]); | ||
160 | |||
161 | $entry = $client->getContainer() | ||
162 | ->get('doctrine.orm.entity_manager') | ||
163 | ->getRepository('WallabagCoreBundle:Entry') | ||
164 | ->findOneWithTags(); | ||
165 | |||
166 | if (!$entry) { | ||
167 | $this->markTestSkipped('No content found in db.'); | ||
168 | } | ||
169 | |||
170 | $tags = array(); | ||
171 | foreach ($entry->getTags() as $tag) { | ||
172 | $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel()); | ||
173 | } | ||
174 | |||
175 | $client->request('GET', '/api/entries/'.$entry->getId().'/tags', array(), array(), $headers); | ||
176 | |||
177 | $this->assertEquals(json_encode($tags), $client->getResponse()->getContent()); | ||
178 | } | ||
153 | } | 179 | } |