]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Change ManyToMany between entry & tag 2122/head
authorJeremy Benoist <jeremy.benoist@gmail.com>
Mon, 30 May 2016 12:34:11 +0000 (14:34 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Tue, 31 May 2016 08:01:03 +0000 (10:01 +0200)
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

src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php

index ebfebfea5c0b7400b50768f385ed1a0a8b2e81d4..03be96670adccabc63e36a62d125553fd47a6c35 100644 (file)
@@ -6,7 +6,6 @@ use Doctrine\Common\DataFixtures\AbstractFixture;
 use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
 use Doctrine\Common\Persistence\ObjectManager;
 use Wallabag\CoreBundle\Entity\Config;
-use Wallabag\CoreBundle\Entity\TaggingRule;
 
 class LoadConfigData extends AbstractFixture implements OrderedFixtureInterface
 {
index 84981414a639e81da28d209758d36bd6fefb7787..ceae78b05556e4005f0c51e35e1eb93a5bf6c57a 100644 (file)
@@ -178,7 +178,15 @@ class Entry
 
     /**
      * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
-     * @ORM\JoinTable
+     * @ORM\JoinTable(
+     *  name="entry_tag",
+     *  joinColumns={
+     *      @ORM\JoinColumn(name="entry_id", referencedColumnName="id")
+     *  },
+     *  inverseJoinColumns={
+     *      @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
+     *  }
+     * )
      *
      * @Groups({"entries_for_user", "export_all"})
      */
@@ -526,13 +534,18 @@ class Entry
             }
         }
 
-        $this->tags[] = $tag;
+        $this->tags->add($tag);
         $tag->addEntry($this);
     }
 
     public function removeTag(Tag $tag)
     {
+        if (!$this->tags->contains($tag)) {
+            return;
+        }
+
         $this->tags->removeElement($tag);
+        $tag->removeEntry($this);
     }
 
     /**
index b4adbbd3eeffdb1e5ee774aa0027cac3d91475e2..4b480ff1cdc4f2c4ebc2b7e4bf4b558edb95eb9a 100644 (file)
@@ -98,9 +98,30 @@ class Tag
         return $this->slug;
     }
 
+    /**
+     * @param Entry $entry
+     */
     public function addEntry(Entry $entry)
     {
-        $this->entries[] = $entry;
+        if ($this->entries->contains($entry)) {
+            return;
+        }
+
+        $this->entries->add($entry);
+        $entry->addTag($this);
+    }
+
+    /**
+     * @param Entry $entry
+     */
+    public function removeEntry(Entry $entry)
+    {
+        if (!$this->entries->contains($entry)) {
+            return;
+        }
+
+        $this->entries->removeElement($entry);
+        $entry->removeTag($this);
     }
 
     public function hasEntry($entry)
index df4c34cd8340481de88657d43ef6426d51635abe..cbba1a57c26d635a157cfe8379ee2f9d0c28a1a2 100644 (file)
@@ -163,7 +163,7 @@ class EntryControllerTest extends WallabagCoreTestCase
     /**
      * This test will require an internet connection.
      */
-    public function testPostNewThatWillBeTaggued()
+    public function testPostNewThatWillBeTagged()
     {
         $this->logInAs('admin');
         $client = $this->getClient();
@@ -181,8 +181,7 @@ class EntryControllerTest extends WallabagCoreTestCase
         $client->submit($form, $data);
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
-
-        $client->followRedirect();
+        $this->assertContains('/', $client->getResponse()->getTargetUrl());
 
         $em = $client->getContainer()
             ->get('doctrine.orm.entity_manager');
@@ -196,6 +195,35 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $em->remove($entry);
         $em->flush();
+
+        // and now re-submit it to test the cascade persistence for tags after entry removal
+        // related https://github.com/wallabag/wallabag/issues/2121
+        $crawler = $client->request('GET', '/new');
+
+        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+        $form = $crawler->filter('form[name=entry]')->form();
+
+        $data = [
+            'entry[url]' => $url = 'https://github.com/wallabag/wallabag/tree/master',
+        ];
+
+        $client->submit($form, $data);
+
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('/', $client->getResponse()->getTargetUrl());
+
+        $entry = $em
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findOneByUrl($url);
+
+        $tags = $entry->getTags();
+
+        $this->assertCount(1, $tags);
+        $this->assertEquals('wallabag', $tags[0]->getLabel());
+
+        $em->remove($entry);
+        $em->flush();
     }
 
     public function testArchive()