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);
- }
+ ->where('e.user = :userId')->setParameter('userId', $userId)
+ ->groupBy('t.slug')
+ ->getQuery();
- /**
- * Find Tags and return a Pager.
- *
- * @param int $userId
- *
- * @return Pagerfanta
- */
- public function findTags($userId)
- {
- $qb = $this->getQbForAllTags($userId);
-
- $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.
*
* @param int $userId
*
*/
public function findAllTags($userId)
{
- return $this->getQbForAllTags($userId)
+ return $this->createQueryBuilder('t')
+ ->select('t.slug', 't.label', 't.id')
+ ->leftJoin('t.entries', 'e')
+ ->where('e.user = :userId')->setParameter('userId', $userId)
+ ->groupBy('t.slug')
+ ->addGroupBy('t.label')
+ ->addGroupBy('t.id')
->getQuery()
- ->getResult();
+ ->getArrayResult();
}
/**