]>
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. |
b0de88f7 JB |
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. | |
faa86e06 JB |
40 | * |
41 | * @param int $userId | |
42 | * | |
43 | * @return array | |
44 | */ | |
28bb4890 | 45 | public function findAllTags($userId) |
7244d6cb | 46 | { |
b0de88f7 JB |
47 | $ids = $this->createQueryBuilder('t') |
48 | ->select('t.id') | |
e9023a16 | 49 | ->leftJoin('t.entries', 'e') |
faa86e06 | 50 | ->where('e.user = :userId')->setParameter('userId', $userId) |
b0de88f7 | 51 | ->groupBy('t.id') |
32755d50 | 52 | ->orderBy('t.slug') |
faa86e06 | 53 | ->getQuery() |
28bb4890 | 54 | ->getArrayResult(); |
567421af | 55 | |
b0de88f7 JB |
56 | $tags = []; |
57 | foreach ($ids as $id) { | |
58 | $tags[] = $this->find($id); | |
59 | } | |
60 | ||
61 | return $tags; | |
206bade5 JB |
62 | } |
63 | ||
567421af TC |
64 | /** |
65 | * Used only in test case to get a tag for our entry. | |
66 | * | |
67 | * @return Tag | |
68 | */ | |
e686a76d | 69 | public function findOneByEntryAndTagLabel($entry, $label) |
567421af TC |
70 | { |
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) | |
75 | ->setMaxResults(1) | |
76 | ->getQuery() | |
77 | ->getSingleResult(); | |
78 | } | |
b3dc0749 | 79 | } |