]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Avoid orphan tags
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 8 Oct 2016 09:05:03 +0000 (11:05 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 22 Oct 2016 11:13:07 +0000 (13:13 +0200)
src/Wallabag/CoreBundle/Controller/ConfigController.php

index faa85d1686919b15bfb7d064e4cfc22889770389..e2484064a3e85c1d7b90c5ffe6a634d996994f8e 100644 (file)
@@ -251,9 +251,11 @@ class ConfigController extends Controller
                 // otherwise they won't be removed ...
                 if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
                     $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId());
-                    $this->removeAllTagsByUserId($this->getUser()->getId());
                 }
 
+                // manually remove tags first to avoid orphan tag
+                $this->removeAllTagsByUserId($this->getUser()->getId());
+
                 $this->getDoctrine()
                     ->getRepository('WallabagCoreBundle:Entry')
                     ->removeAllByUserId($this->getUser()->getId());
@@ -268,7 +270,7 @@ class ConfigController extends Controller
     }
 
     /**
-     * Remove all tags for a given user.
+     * Remove all tags for a given user and cleanup orphan tags
      *
      * @param  int $userId
      */
@@ -283,6 +285,16 @@ class ConfigController extends Controller
         $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
             ->removeTags($userId, $tags);
+
+        $em = $this->getDoctrine()->getManager();
+
+        foreach ($tags as $tag) {
+            if (count($tag->getEntries()) === 0) {
+                $em->remove($tag);
+            }
+        }
+
+        $em->flush();
     }
 
     /**