3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Wallabag\CoreBundle\Entity\Tag
;
8 class TagRepository
extends EntityRepository
11 * Count all tags per user.
14 * @param int $cacheLifeTime Duration of the cache for this query
18 public function countAllTags($userId, $cacheLifeTime = null)
20 $query = $this->createQueryBuilder('t')
22 ->leftJoin('t.entries', 'e')
23 ->where('e.user = :userId')->setParameter('userId', $userId)
27 if (null !== $cacheLifeTime) {
28 $query->useQueryCache(true);
29 $query->useResultCache(true);
30 $query->setResultCacheLifetime($cacheLifeTime);
33 return \
count($query->getArrayResult());
37 * Find all tags per user.
38 * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results.
39 * Once we have all tags id, we can safely request them one by one.
40 * This'll still be fastest than the previous query.
46 public function findAllTags($userId)
48 $ids = $this->createQueryBuilder('t')
50 ->leftJoin('t.entries', 'e')
51 ->where('e.user = :userId')->setParameter('userId', $userId)
58 foreach ($ids as $id) {
59 $tags[] = $this->find($id);
66 * Find all tags (flat) per user with nb entries.
72 public function findAllFlatTagsWithNbEntries($userId)
74 return $this->createQueryBuilder('t')
75 ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
77 ->leftJoin('t.entries', 'e')
78 ->where('e.user = :userId')
81 ->setParameter('userId', $userId)
87 * Used only in test case to get a tag for our entry.
91 public function findOneByEntryAndTagLabel($entry, $label)
93 return $this->createQueryBuilder('t')
94 ->leftJoin('t.entries', 'e')
95 ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
96 ->andWhere('t.label = :label')->setParameter('label', $label)
102 public function findForArchivedArticlesByUser($userId)
104 $ids = $this->createQueryBuilder('t')
106 ->leftJoin('t.entries', 'e')
107 ->where('e.user = :userId')->setParameter('userId', $userId)
108 ->andWhere('e.isArchived = true')
115 foreach ($ids as $id) {
116 $tags[] = $this->find($id);