]>
Commit | Line | Data |
---|---|---|
b3dc0749 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Repository; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
52b84c11 | 6 | use Wallabag\CoreBundle\Entity\Tag; |
b3dc0749 | 7 | |
6d37a7e6 | 8 | class TagRepository extends EntityRepository |
b3dc0749 | 9 | { |
7244d6cb | 10 | /** |
28987583 | 11 | * Count all tags per user. |
7244d6cb | 12 | * |
fc732227 | 13 | * @param int $userId |
faa86e06 | 14 | * @param int $cacheLifeTime Duration of the cache for this query |
7244d6cb | 15 | * |
28987583 | 16 | * @return int |
7244d6cb | 17 | */ |
28987583 | 18 | public function countAllTags($userId, $cacheLifeTime = null) |
faa86e06 JB |
19 | { |
20 | $query = $this->createQueryBuilder('t') | |
28987583 | 21 | ->select('t.slug') |
faa86e06 JB |
22 | ->leftJoin('t.entries', 'e') |
23 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
24 | ->groupBy('t.slug') | |
25 | ->getQuery(); | |
26 | ||
27 | if (null !== $cacheLifeTime) { | |
28 | $query->useQueryCache(true); | |
29 | $query->useResultCache(true); | |
30 | $query->setResultCacheLifetime($cacheLifeTime); | |
31 | } | |
32 | ||
28987583 | 33 | return count($query->getArrayResult()); |
faa86e06 JB |
34 | } |
35 | ||
36 | /** | |
28bb4890 | 37 | * Find all tags per user. |
b0de88f7 JB |
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. | |
faa86e06 JB |
41 | * |
42 | * @param int $userId | |
43 | * | |
44 | * @return array | |
45 | */ | |
28bb4890 | 46 | public function findAllTags($userId) |
7244d6cb | 47 | { |
b0de88f7 JB |
48 | $ids = $this->createQueryBuilder('t') |
49 | ->select('t.id') | |
e9023a16 | 50 | ->leftJoin('t.entries', 'e') |
faa86e06 | 51 | ->where('e.user = :userId')->setParameter('userId', $userId) |
b0de88f7 | 52 | ->groupBy('t.id') |
32755d50 | 53 | ->orderBy('t.slug') |
faa86e06 | 54 | ->getQuery() |
28bb4890 | 55 | ->getArrayResult(); |
567421af | 56 | |
b0de88f7 JB |
57 | $tags = []; |
58 | foreach ($ids as $id) { | |
59 | $tags[] = $this->find($id); | |
60 | } | |
61 | ||
62 | return $tags; | |
206bade5 JB |
63 | } |
64 | ||
567421af TC |
65 | /** |
66 | * Used only in test case to get a tag for our entry. | |
67 | * | |
68 | * @return Tag | |
69 | */ | |
e686a76d | 70 | public function findOneByEntryAndTagLabel($entry, $label) |
567421af TC |
71 | { |
72 | return $this->createQueryBuilder('t') | |
73 | ->leftJoin('t.entries', 'e') | |
74 | ->where('e.id = :entryId')->setParameter('entryId', $entry->getId()) | |
75 | ->andWhere('t.label = :label')->setParameter('label', $label) | |
76 | ->setMaxResults(1) | |
77 | ->getQuery() | |
78 | ->getSingleResult(); | |
79 | } | |
6da1aebc | 80 | |
13a592a1 | 81 | public function findForArchivedArticlesByUser($userId) |
6da1aebc TC |
82 | { |
83 | $ids = $this->createQueryBuilder('t') | |
84 | ->select('t.id') | |
85 | ->leftJoin('t.entries', 'e') | |
86 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
87 | ->andWhere('e.isArchived = true') | |
88 | ->groupBy('t.id') | |
89 | ->orderBy('t.slug') | |
90 | ->getQuery() | |
91 | ->getArrayResult(); | |
92 | ||
93 | $tags = []; | |
94 | foreach ($ids as $id) { | |
95 | $tags[] = $this->find($id); | |
96 | } | |
97 | ||
98 | return $tags; | |
99 | } | |
b3dc0749 | 100 | } |