]>
Commit | Line | Data |
---|---|---|
b3dc0749 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Repository; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
b3dc0749 | 6 | |
6d37a7e6 | 7 | class TagRepository extends EntityRepository |
b3dc0749 | 8 | { |
7244d6cb | 9 | /** |
28987583 | 10 | * Count all tags per user. |
7244d6cb | 11 | * |
fc732227 | 12 | * @param int $userId |
faa86e06 | 13 | * @param int $cacheLifeTime Duration of the cache for this query |
7244d6cb | 14 | * |
28987583 | 15 | * @return int |
7244d6cb | 16 | */ |
28987583 | 17 | public function countAllTags($userId, $cacheLifeTime = null) |
faa86e06 JB |
18 | { |
19 | $query = $this->createQueryBuilder('t') | |
28987583 | 20 | ->select('t.slug') |
faa86e06 JB |
21 | ->leftJoin('t.entries', 'e') |
22 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
23 | ->groupBy('t.slug') | |
24 | ->getQuery(); | |
25 | ||
26 | if (null !== $cacheLifeTime) { | |
27 | $query->useQueryCache(true); | |
28 | $query->useResultCache(true); | |
29 | $query->setResultCacheLifetime($cacheLifeTime); | |
30 | } | |
31 | ||
28987583 | 32 | return count($query->getArrayResult()); |
faa86e06 JB |
33 | } |
34 | ||
35 | /** | |
28bb4890 | 36 | * Find all tags per user. |
faa86e06 JB |
37 | * |
38 | * @param int $userId | |
39 | * | |
40 | * @return array | |
41 | */ | |
28bb4890 | 42 | public function findAllTags($userId) |
7244d6cb | 43 | { |
e9023a16 | 44 | return $this->createQueryBuilder('t') |
28bb4890 | 45 | ->select('t.slug', 't.label', 't.id') |
e9023a16 | 46 | ->leftJoin('t.entries', 'e') |
faa86e06 | 47 | ->where('e.user = :userId')->setParameter('userId', $userId) |
28bb4890 JB |
48 | ->groupBy('t.slug') |
49 | ->addGroupBy('t.label') | |
50 | ->addGroupBy('t.id') | |
faa86e06 | 51 | ->getQuery() |
28bb4890 | 52 | ->getArrayResult(); |
7244d6cb | 53 | } |
567421af TC |
54 | |
55 | /** | |
56 | * Used only in test case to get a tag for our entry. | |
57 | * | |
58 | * @return Tag | |
59 | */ | |
e686a76d | 60 | public function findOneByEntryAndTagLabel($entry, $label) |
567421af TC |
61 | { |
62 | return $this->createQueryBuilder('t') | |
63 | ->leftJoin('t.entries', 'e') | |
64 | ->where('e.id = :entryId')->setParameter('entryId', $entry->getId()) | |
65 | ->andWhere('t.label = :label')->setParameter('label', $label) | |
66 | ->setMaxResults(1) | |
67 | ->getQuery() | |
68 | ->getSingleResult(); | |
69 | } | |
b3dc0749 | 70 | } |