3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
7 class TagRepository
extends EntityRepository
10 * Count all tags per user.
13 * @param int $cacheLifeTime Duration of the cache for this query
17 public function countAllTags($userId, $cacheLifeTime = null)
19 $query = $this->createQueryBuilder('t')
21 ->leftJoin('t.entries', 'e')
22 ->where('e.user = :userId')->setParameter('userId', $userId)
26 if (null !== $cacheLifeTime) {
27 $query->useQueryCache(true);
28 $query->useResultCache(true);
29 $query->setResultCacheLifetime($cacheLifeTime);
32 return count($query->getArrayResult());
36 * Find all tags per user.
37 * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results.
38 * Once we have all tags id, we can safely request them one by one.
39 * This'll still be fastest than the previous query.
45 public function findAllTags($userId)
47 $ids = $this->createQueryBuilder('t')
49 ->leftJoin('t.entries', 'e')
50 ->where('e.user = :userId')->setParameter('userId', $userId)
57 foreach ($ids as $id) {
58 $tags[] = $this->find($id);
65 * Used only in test case to get a tag for our entry.
69 public function findOneByEntryAndTagLabel($entry, $label)
71 return $this->createQueryBuilder('t')
72 ->leftJoin('t.entries', 'e')
73 ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
74 ->andWhere('t.label = :label')->setParameter('label', $label)
80 public function findTagsForArchivedArticles($userId)
82 $ids = $this->createQueryBuilder('t')
84 ->leftJoin('t.entries', 'e')
85 ->where('e.user = :userId')->setParameter('userId', $userId)
86 ->andWhere('e.isArchived = true')
93 foreach ($ids as $id) {
94 $tags[] = $this->find($id);