From 935e9fffb4083f83e86c4e97a32bc2d9bccd677f Mon Sep 17 00:00:00 2001 From: Nicolas Hart Date: Sun, 6 Aug 2017 21:58:14 +0200 Subject: [PATCH] Reduce number of queries on tag list --- .../CoreBundle/Controller/TagController.php | 21 ++----------------- .../CoreBundle/Repository/EntryRepository.php | 20 ------------------ .../CoreBundle/Repository/TagRepository.php | 21 +++++++++++++++++++ 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index f2ca58c6..be2dff98 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php @@ -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, ]); } diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 7f35bb9a..d70d6ca6 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -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. diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index 213283e5..5c45211f 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -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. * -- 2.41.0