X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FTagRepository.php;h=81445989b71c97fc06a13e22f8aaada1b81d871d;hb=5a619812ca3eb05a82a023ccdaee13501eb8d45f;hp=afeb985b30c272c9b9f76d84540b15eaf230f172;hpb=06c190887fd38c314db8d240ab2e46f5777ed59e;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index afeb985b..81445989 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -3,43 +3,40 @@ namespace Wallabag\CoreBundle\Repository; use Doctrine\ORM\EntityRepository; -use Pagerfanta\Adapter\DoctrineORMAdapter; -use Pagerfanta\Pagerfanta; class TagRepository extends EntityRepository { /** - * Return only the QueryBuilder to retrieve all tags for a given user. + * Count all tags per user. * * @param int $userId + * @param int $cacheLifeTime Duration of the cache for this query * - * @return QueryBuilder + * @return int */ - private function getQbForAllTags($userId) + public function countAllTags($userId, $cacheLifeTime = null) { - return $this->createQueryBuilder('t') + $query = $this->createQueryBuilder('t') + ->select('t.slug') ->leftJoin('t.entries', 'e') - ->where('e.user = :userId')->setParameter('userId', $userId); - } - - /** - * Find Tags and return a Pager. - * - * @param int $userId - * - * @return Pagerfanta - */ - public function findTags($userId) - { - $qb = $this->getQbForAllTags($userId); + ->where('e.user = :userId')->setParameter('userId', $userId) + ->groupBy('t.slug') + ->getQuery(); - $pagerAdapter = new DoctrineORMAdapter($qb); + if (null !== $cacheLifeTime) { + $query->useQueryCache(true); + $query->useResultCache(true); + $query->setResultCacheLifetime($cacheLifeTime); + } - return new Pagerfanta($pagerAdapter); + return count($query->getArrayResult()); } /** - * Find Tags. + * Find all tags per user. + * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results. + * Once we have all tags id, we can safely request them one by one. + * This'll still be fastest than the previous query. * * @param int $userId * @@ -47,9 +44,20 @@ class TagRepository extends EntityRepository */ public function findAllTags($userId) { - return $this->getQbForAllTags($userId) + $ids = $this->createQueryBuilder('t') + ->select('t.id') + ->leftJoin('t.entries', 'e') + ->where('e.user = :userId')->setParameter('userId', $userId) + ->groupBy('t.id') ->getQuery() - ->getResult(); + ->getArrayResult(); + + $tags = []; + foreach ($ids as $id) { + $tags[] = $this->find($id); + } + + return $tags; } /**