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();
+
+ if (null !== $cacheLifeTime) {
+ $query->useQueryCache(true);
+ $query->useResultCache(true);
+ $query->setResultCacheLifetime($cacheLifeTime);
+ }
+
+ 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();
}
/**