]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Fix tags count in menu
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 25 Sep 2016 09:21:13 +0000 (11:21 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 25 Sep 2016 10:03:49 +0000 (12:03 +0200)
Move enable cache for Tag in the Entity because function `find*` should return result and not a Query

src/Wallabag/ApiBundle/Controller/WallabagRestController.php
src/Wallabag/CoreBundle/Controller/TagController.php
src/Wallabag/CoreBundle/Repository/TagRepository.php
src/Wallabag/CoreBundle/Twig/WallabagExtension.php

index fb7c6c1f14a0ff90f0601241f043c7047b89edb7..dd17ef976d38a6a3f8f49ae3b639a5ba5e65a94a 100644 (file)
@@ -322,9 +322,7 @@ class WallabagRestController extends FOSRestController
 
         $tags = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Tag')
-            ->findAllTags($this->getUser()->getId())
-            ->getQuery()
-            ->getResult();
+            ->findAllTagsWithEntries($this->getUser()->getId());
 
         $json = $this->get('serializer')->serialize($tags, 'json');
 
index bc95a4d34ac4e6bda9e265c120b43d63397b4e3e..07cd3edb3e333a1cd7f7ea86de927844d6a4349c 100644 (file)
@@ -84,16 +84,11 @@ class TagController extends Controller
     {
         $tags = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Tag')
-            ->findAllTags($this->getUser()->getId())
-            ->getQuery()
-            ->getResult();
-
-        return $this->render(
-            'WallabagCoreBundle:Tag:tags.html.twig',
-            [
-                'tags' => $tags,
-            ]
-        );
+            ->findAllTagsWithEntries($this->getUser()->getId());
+
+        return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
+            'tags' => $tags,
+        ]);
     }
 
     /**
@@ -127,13 +122,10 @@ class TagController extends Controller
             }
         }
 
-        return $this->render(
-            'WallabagCoreBundle:Entry:entries.html.twig',
-            [
-                'form' => null,
-                'entries' => $entries,
-                'currentPage' => $page,
-            ]
-        );
+        return $this->render('WallabagCoreBundle:Entry:entries.html.twig',[
+            'form' => null,
+            'entries' => $entries,
+            'currentPage' => $page,
+        ]);
     }
 }
index 41f616079dd53f69032512adf6a1bfb176b98657..f5c4ea6a036bea3bcc56987ec475d8495d17533d 100644 (file)
@@ -7,17 +7,45 @@ use Doctrine\ORM\EntityRepository;
 class TagRepository extends EntityRepository
 {
     /**
-     * Find Tags.
+     * Find all tags per user.
      *
      * @param int $userId
+     * @param int $cacheLifeTime Duration of the cache for this query
      *
      * @return array
      */
-    public function findAllTags($userId)
+    public function findAllTags($userId, $cacheLifeTime = null)
+    {
+        $query = $this->createQueryBuilder('t')
+            ->select('t')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.slug')
+            ->getQuery();
+
+        if (null !== $cacheLifeTime) {
+            $query->useQueryCache(true);
+            $query->useResultCache(true);
+            $query->setResultCacheLifetime($cacheLifeTime);
+        }
+
+        return $query->getArrayResult();
+    }
+
+    /**
+     * Find all tags with associated entries per user.
+     *
+     * @param int $userId
+     *
+     * @return array
+     */
+    public function findAllTagsWithEntries($userId)
     {
         return $this->createQueryBuilder('t')
             ->leftJoin('t.entries', 'e')
-            ->where('e.user = :userId')->setParameter('userId', $userId);
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->getQuery()
+            ->getResult();
     }
 
     /**
index 3780b13e7db5884abf3b9e156dff8db0e0b8c397..0a6896c95367f6b3ad0006875e8ca8cf9422bd92 100644 (file)
@@ -85,10 +85,11 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
             ->groupBy('e.id')
             ->getQuery();
 
-        $data = $this->enableCache($query)
-            ->getArrayResult();
+        $query->useQueryCache(true);
+        $query->useResultCache(true);
+        $query->setResultCacheLifetime($this->lifeTime);
 
-        return count($data);
+        return count($query->getArrayResult());
     }
 
     /**
@@ -104,30 +105,11 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
             return 0;
         }
 
-        $qb = $this->tagRepository->findAllTags($user->getId());
-
-        $data = $this->enableCache($qb->getQuery())
-            ->getArrayResult();
+        $data = $this->tagRepository->findAllTags($user->getId());
 
         return count($data);
     }
 
-    /**
-     * Enable cache for a query.
-     *
-     * @param Query $query
-     *
-     * @return Query
-     */
-    private function enableCache(Query $query)
-    {
-        $query->useQueryCache(true);
-        $query->useResultCache(true);
-        $query->setResultCacheLifetime($this->lifeTime);
-
-        return $query;
-    }
-
     public function getName()
     {
         return 'wallabag_extension';