]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Reduce number of queries on tag list 3314/head
authorNicolas Hart <contact@nclshart.net>
Sun, 6 Aug 2017 19:58:14 +0000 (21:58 +0200)
committerNicolas Hart <contact@nclshart.net>
Sun, 6 Aug 2017 21:02:32 +0000 (23:02 +0200)
src/Wallabag/CoreBundle/Controller/TagController.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
src/Wallabag/CoreBundle/Repository/TagRepository.php

index f2ca58c6af004ee74d1b42ccc7d6865be38a8507..be2dff98b3b62fcb69849f505fba3be6e7659929 100644 (file)
@@ -84,28 +84,11 @@ class TagController extends Controller
      */
     public function showTagAction()
     {
-        $repository = $this->get('wallabag_core.entry_repository');
         $tags = $this->get('wallabag_core.tag_repository')
-            ->findAllTags($this->getUser()->getId());
-
-        $flatTags = [];
-
-        foreach ($tags as $tag) {
-            $nbEntries = $repository->countAllEntriesByUserIdAndTagId(
-                $this->getUser()->getId(),
-                $tag->getId()
-            );
-
-            $flatTags[] = [
-                'id' => $tag->getId(),
-                'label' => $tag->getLabel(),
-                'slug' => $tag->getSlug(),
-                'nbEntries' => $nbEntries,
-            ];
-        }
+            ->findAllFlatTagsWithNbEntries($this->getUser()->getId());
 
         return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
-            'tags' => $flatTags,
+            'tags' => $tags,
         ]);
     }
 
index 7f35bb9a19b6746ea665d34fab6de9acc4d853ec..d70d6ca6056d7e96b5445658326f809504694a26 100644 (file)
@@ -329,26 +329,6 @@ class EntryRepository extends EntityRepository
         return (int) $qb->getQuery()->getSingleScalarResult();
     }
 
-    /**
-     * Count all entries for a tag and a user.
-     *
-     * @param int $userId
-     * @param int $tagId
-     *
-     * @return int
-     */
-    public function countAllEntriesByUserIdAndTagId($userId, $tagId)
-    {
-        $qb = $this->createQueryBuilder('e')
-            ->select('count(e.id)')
-            ->leftJoin('e.tags', 't')
-            ->where('e.user=:userId')->setParameter('userId', $userId)
-            ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
-        ;
-
-        return (int) $qb->getQuery()->getSingleScalarResult();
-    }
-
     /**
      * Remove all entries for a user id.
      * Used when a user want to reset all informations.
index 213283e5d6d8b1da418a2eba536823fc152ec83e..5c45211f6beb1e2820e9ed5ddba79f95c10a04df 100644 (file)
@@ -62,6 +62,27 @@ class TagRepository extends EntityRepository
         return $tags;
     }
 
+    /**
+     * Find all tags (flat) per user with nb entries.
+     *
+     * @param int $userId
+     *
+     * @return array
+     */
+    public function findAllFlatTagsWithNbEntries($userId)
+    {
+        return $this->createQueryBuilder('t')
+            ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
+            ->distinct(true)
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')
+            ->groupBy('t.id')
+            ->orderBy('t.slug')
+            ->setParameter('userId', $userId)
+            ->getQuery()
+            ->getArrayResult();
+    }
+
     /**
      * Used only in test case to get a tag for our entry.
      *