TagRepository: refactor query builder for queries by userId

Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
This commit is contained in:
Kevin Decherf 2018-12-29 19:42:37 +01:00
parent bafb9744c8
commit 6708bf238d

View File

@ -3,6 +3,7 @@
namespace Wallabag\CoreBundle\Repository; namespace Wallabag\CoreBundle\Repository;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Entity\Tag;
class TagRepository extends EntityRepository class TagRepository extends EntityRepository
@ -45,12 +46,8 @@ class TagRepository extends EntityRepository
*/ */
public function findAllTags($userId) public function findAllTags($userId)
{ {
$ids = $this->createQueryBuilder('t') $ids = $this->getQueryBuilderByUser($userId)
->select('t.id') ->select('t.id')
->leftJoin('t.entries', 'e')
->where('e.user = :userId')->setParameter('userId', $userId)
->groupBy('t.id')
->orderBy('t.slug')
->getQuery() ->getQuery()
->getArrayResult(); ->getArrayResult();
@ -71,14 +68,9 @@ class TagRepository extends EntityRepository
*/ */
public function findAllFlatTagsWithNbEntries($userId) public function findAllFlatTagsWithNbEntries($userId)
{ {
return $this->createQueryBuilder('t') return $this->getQueryBuilderByUser($userId)
->select('t.id, t.label, t.slug, count(e.id) as nbEntries') ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
->distinct(true) ->distinct(true)
->leftJoin('t.entries', 'e')
->where('e.user = :userId')
->groupBy('t.id')
->orderBy('t.slug')
->setParameter('userId', $userId)
->getQuery() ->getQuery()
->getArrayResult(); ->getArrayResult();
} }
@ -101,13 +93,9 @@ class TagRepository extends EntityRepository
public function findForArchivedArticlesByUser($userId) public function findForArchivedArticlesByUser($userId)
{ {
$ids = $this->createQueryBuilder('t') $ids = $this->getQueryBuilderByUser($userId)
->select('t.id') ->select('t.id')
->leftJoin('t.entries', 'e')
->where('e.user = :userId')->setParameter('userId', $userId)
->andWhere('e.isArchived = true') ->andWhere('e.isArchived = true')
->groupBy('t.id')
->orderBy('t.slug')
->getQuery() ->getQuery()
->getArrayResult(); ->getArrayResult();
@ -118,4 +106,20 @@ class TagRepository extends EntityRepository
return $tags; 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');
}
} }