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)
56 foreach ($ids as $id) {
57 $tags[] = $this->find($id);
64 * Used only in test case to get a tag for our entry.
68 public function findOneByEntryAndTagLabel($entry, $label)
70 return $this->createQueryBuilder('t')
71 ->leftJoin('t.entries', 'e')
72 ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
73 ->andWhere('t.label = :label')->setParameter('label', $label)