From 6708bf238de46d7ce861e3c0eeb6a9b4623931ed Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sat, 29 Dec 2018 19:42:37 +0100 Subject: [PATCH] TagRepository: refactor query builder for queries by userId Signed-off-by: Kevin Decherf --- .../CoreBundle/Repository/TagRepository.php | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index 3ae9d414..bd2d9f97 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -3,6 +3,7 @@ namespace Wallabag\CoreBundle\Repository; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\QueryBuilder; use Wallabag\CoreBundle\Entity\Tag; class TagRepository extends EntityRepository @@ -45,12 +46,8 @@ class TagRepository extends EntityRepository */ public function findAllTags($userId) { - $ids = $this->createQueryBuilder('t') + $ids = $this->getQueryBuilderByUser($userId) ->select('t.id') - ->leftJoin('t.entries', 'e') - ->where('e.user = :userId')->setParameter('userId', $userId) - ->groupBy('t.id') - ->orderBy('t.slug') ->getQuery() ->getArrayResult(); @@ -71,14 +68,9 @@ class TagRepository extends EntityRepository */ public function findAllFlatTagsWithNbEntries($userId) { - return $this->createQueryBuilder('t') + return $this->getQueryBuilderByUser($userId) ->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(); } @@ -101,13 +93,9 @@ class TagRepository extends EntityRepository public function findForArchivedArticlesByUser($userId) { - $ids = $this->createQueryBuilder('t') + $ids = $this->getQueryBuilderByUser($userId) ->select('t.id') - ->leftJoin('t.entries', 'e') - ->where('e.user = :userId')->setParameter('userId', $userId) ->andWhere('e.isArchived = true') - ->groupBy('t.id') - ->orderBy('t.slug') ->getQuery() ->getArrayResult(); @@ -118,4 +106,20 @@ class TagRepository extends EntityRepository return $tags; } + + /** + * Retrieve a sorted list of tags used by a user. + * + * @param int $userId + * + * @return QueryBuilder + */ + private function getQueryBuilderByUser($userId) + { + return $this->createQueryBuilder('t') + ->leftJoin('t.entries', 'e') + ->where('e.user = :userId')->setParameter('userId', $userId) + ->groupBy('t.id') + ->orderBy('t.slug'); + } } -- 2.41.0