3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Doctrine\ORM\QueryBuilder
;
7 use Wallabag\CoreBundle\Entity\Tag
;
9 class TagRepository
extends EntityRepository
12 * Count all tags per user.
15 * @param int $cacheLifeTime Duration of the cache for this query
19 public function countAllTags($userId, $cacheLifeTime = null)
21 $query = $this->createQueryBuilder('t')
23 ->leftJoin('t.entries', 'e')
24 ->where('e.user = :userId')->setParameter('userId', $userId)
28 if (null !== $cacheLifeTime) {
29 $query->useQueryCache(true);
30 $query->useResultCache(true);
31 $query->setResultCacheLifetime($cacheLifeTime);
34 return \
count($query->getArrayResult());
38 * Find all tags per user.
39 * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results.
40 * Once we have all tags id, we can safely request them one by one.
41 * This'll still be fastest than the previous query.
47 public function findAllTags($userId)
49 $ids = $this->getQueryBuilderByUser($userId)
55 foreach ($ids as $id) {
56 $tags[] = $this->find($id);
63 * Find all tags (flat) per user with nb entries.
69 public function findAllFlatTagsWithNbEntries($userId)
71 return $this->getQueryBuilderByUser($userId)
72 ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
78 public function findByLabelsAndUser($labels, $userId)
80 $qb = $this->getQueryBuilderByUser($userId)
83 $ids = $qb->andWhere($qb->expr()->in('t.label', $labels))
88 foreach ($ids as $id) {
89 $tags[] = $this->find($id);
96 * Used only in test case to get a tag for our entry.
100 public function findOneByEntryAndTagLabel($entry, $label)
102 return $this->createQueryBuilder('t')
103 ->leftJoin('t.entries', 'e')
104 ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
105 ->andWhere('t.label = :label')->setParameter('label', $label)
111 public function findForArchivedArticlesByUser($userId)
113 $ids = $this->getQueryBuilderByUser($userId)
115 ->andWhere('e.isArchived = true')
120 foreach ($ids as $id) {
121 $tags[] = $this->find($id);
128 * Retrieve a sorted list of tags used by a user.
132 * @return QueryBuilder
134 private function getQueryBuilderByUser($userId)
136 return $this->createQueryBuilder('t')
137 ->leftJoin('t.entries', 'e')
138 ->where('e.user = :userId')->setParameter('userId', $userId)