diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-05-30 14:34:11 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-05-31 10:01:03 +0200 |
commit | 3be047456d6f91d8ef5404c9c7698e0d1f9a057d (patch) | |
tree | 6914fc02866751594b2d73387adb826bbce980f8 | |
parent | 39ba51ca1af3085c3a7eb6dabdfae38c5034db1b (diff) | |
download | wallabag-3be047456d6f91d8ef5404c9c7698e0d1f9a057d.tar.gz wallabag-3be047456d6f91d8ef5404c9c7698e0d1f9a057d.tar.zst wallabag-3be047456d6f91d8ef5404c9c7698e0d1f9a057d.zip |
Change ManyToMany between entry & tag
Following https://gist.github.com/Ocramius/3121916
Be sure to remove the related entity when removing an entity.
Let say you have Entry -> EntryTag -> Tag.
If you remove the entry:
- before that commit, the EntryTag will stay (at least using SQLite).
- with that commit, the related entity is removed
4 files changed, 68 insertions, 7 deletions
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php index ebfebfea..03be9667 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php | |||
@@ -6,7 +6,6 @@ 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\Config; | 8 | use Wallabag\CoreBundle\Entity\Config; |
9 | use Wallabag\CoreBundle\Entity\TaggingRule; | ||
10 | 9 | ||
11 | class LoadConfigData extends AbstractFixture implements OrderedFixtureInterface | 10 | class LoadConfigData extends AbstractFixture implements OrderedFixtureInterface |
12 | { | 11 | { |
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 84981414..ceae78b0 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php | |||
@@ -178,7 +178,15 @@ class Entry | |||
178 | 178 | ||
179 | /** | 179 | /** |
180 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) | 180 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) |
181 | * @ORM\JoinTable | 181 | * @ORM\JoinTable( |
182 | * name="entry_tag", | ||
183 | * joinColumns={ | ||
184 | * @ORM\JoinColumn(name="entry_id", referencedColumnName="id") | ||
185 | * }, | ||
186 | * inverseJoinColumns={ | ||
187 | * @ORM\JoinColumn(name="tag_id", referencedColumnName="id") | ||
188 | * } | ||
189 | * ) | ||
182 | * | 190 | * |
183 | * @Groups({"entries_for_user", "export_all"}) | 191 | * @Groups({"entries_for_user", "export_all"}) |
184 | */ | 192 | */ |
@@ -526,13 +534,18 @@ class Entry | |||
526 | } | 534 | } |
527 | } | 535 | } |
528 | 536 | ||
529 | $this->tags[] = $tag; | 537 | $this->tags->add($tag); |
530 | $tag->addEntry($this); | 538 | $tag->addEntry($this); |
531 | } | 539 | } |
532 | 540 | ||
533 | public function removeTag(Tag $tag) | 541 | public function removeTag(Tag $tag) |
534 | { | 542 | { |
543 | if (!$this->tags->contains($tag)) { | ||
544 | return; | ||
545 | } | ||
546 | |||
535 | $this->tags->removeElement($tag); | 547 | $this->tags->removeElement($tag); |
548 | $tag->removeEntry($this); | ||
536 | } | 549 | } |
537 | 550 | ||
538 | /** | 551 | /** |
diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index b4adbbd3..4b480ff1 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php | |||
@@ -98,9 +98,30 @@ class Tag | |||
98 | return $this->slug; | 98 | return $this->slug; |
99 | } | 99 | } |
100 | 100 | ||
101 | /** | ||
102 | * @param Entry $entry | ||
103 | */ | ||
101 | public function addEntry(Entry $entry) | 104 | public function addEntry(Entry $entry) |
102 | { | 105 | { |
103 | $this->entries[] = $entry; | 106 | if ($this->entries->contains($entry)) { |
107 | return; | ||
108 | } | ||
109 | |||
110 | $this->entries->add($entry); | ||
111 | $entry->addTag($this); | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * @param Entry $entry | ||
116 | */ | ||
117 | public function removeEntry(Entry $entry) | ||
118 | { | ||
119 | if (!$this->entries->contains($entry)) { | ||
120 | return; | ||
121 | } | ||
122 | |||
123 | $this->entries->removeElement($entry); | ||
124 | $entry->removeTag($this); | ||
104 | } | 125 | } |
105 | 126 | ||
106 | public function hasEntry($entry) | 127 | public function hasEntry($entry) |
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index df4c34cd..cbba1a57 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php | |||
@@ -163,7 +163,7 @@ class EntryControllerTest extends WallabagCoreTestCase | |||
163 | /** | 163 | /** |
164 | * This test will require an internet connection. | 164 | * This test will require an internet connection. |
165 | */ | 165 | */ |
166 | public function testPostNewThatWillBeTaggued() | 166 | public function testPostNewThatWillBeTagged() |
167 | { | 167 | { |
168 | $this->logInAs('admin'); | 168 | $this->logInAs('admin'); |
169 | $client = $this->getClient(); | 169 | $client = $this->getClient(); |
@@ -181,8 +181,7 @@ class EntryControllerTest extends WallabagCoreTestCase | |||
181 | $client->submit($form, $data); | 181 | $client->submit($form, $data); |
182 | 182 | ||
183 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | 183 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); |
184 | 184 | $this->assertContains('/', $client->getResponse()->getTargetUrl()); | |
185 | $client->followRedirect(); | ||
186 | 185 | ||
187 | $em = $client->getContainer() | 186 | $em = $client->getContainer() |
188 | ->get('doctrine.orm.entity_manager'); | 187 | ->get('doctrine.orm.entity_manager'); |
@@ -196,6 +195,35 @@ class EntryControllerTest extends WallabagCoreTestCase | |||
196 | 195 | ||
197 | $em->remove($entry); | 196 | $em->remove($entry); |
198 | $em->flush(); | 197 | $em->flush(); |
198 | |||
199 | // and now re-submit it to test the cascade persistence for tags after entry removal | ||
200 | // related https://github.com/wallabag/wallabag/issues/2121 | ||
201 | $crawler = $client->request('GET', '/new'); | ||
202 | |||
203 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
204 | |||
205 | $form = $crawler->filter('form[name=entry]')->form(); | ||
206 | |||
207 | $data = [ | ||
208 | 'entry[url]' => $url = 'https://github.com/wallabag/wallabag/tree/master', | ||
209 | ]; | ||
210 | |||
211 | $client->submit($form, $data); | ||
212 | |||
213 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | ||
214 | $this->assertContains('/', $client->getResponse()->getTargetUrl()); | ||
215 | |||
216 | $entry = $em | ||
217 | ->getRepository('WallabagCoreBundle:Entry') | ||
218 | ->findOneByUrl($url); | ||
219 | |||
220 | $tags = $entry->getTags(); | ||
221 | |||
222 | $this->assertCount(1, $tags); | ||
223 | $this->assertEquals('wallabag', $tags[0]->getLabel()); | ||
224 | |||
225 | $em->remove($entry); | ||
226 | $em->flush(); | ||
199 | } | 227 | } |
200 | 228 | ||
201 | public function testArchive() | 229 | public function testArchive() |