]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Repository; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
6 | use Wallabag\CoreBundle\Entity\Tag; | |
7 | ||
8 | class TagRepository extends EntityRepository | |
9 | { | |
10 | /** | |
11 | * Count all tags per user. | |
12 | * | |
13 | * @param int $userId | |
14 | * @param int $cacheLifeTime Duration of the cache for this query | |
15 | * | |
16 | * @return int | |
17 | */ | |
18 | public function countAllTags($userId, $cacheLifeTime = null) | |
19 | { | |
20 | $query = $this->createQueryBuilder('t') | |
21 | ->select('t.slug') | |
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 | ||
33 | return count($query->getArrayResult()); | |
34 | } | |
35 | ||
36 | /** | |
37 | * Find all tags per user. | |
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. | |
41 | * | |
42 | * @param int $userId | |
43 | * | |
44 | * @return array | |
45 | */ | |
46 | public function findAllTags($userId) | |
47 | { | |
48 | $ids = $this->createQueryBuilder('t') | |
49 | ->select('t.id') | |
50 | ->leftJoin('t.entries', 'e') | |
51 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
52 | ->groupBy('t.id') | |
53 | ->orderBy('t.slug') | |
54 | ->getQuery() | |
55 | ->getArrayResult(); | |
56 | ||
57 | $tags = []; | |
58 | foreach ($ids as $id) { | |
59 | $tags[] = $this->find($id); | |
60 | } | |
61 | ||
62 | return $tags; | |
63 | } | |
64 | ||
65 | /** | |
66 | * Find all tags (flat) per user with nb entries. | |
67 | * | |
68 | * @param int $userId | |
69 | * | |
70 | * @return array | |
71 | */ | |
72 | public function findAllFlatTagsWithNbEntries($userId) | |
73 | { | |
74 | return $this->createQueryBuilder('t') | |
75 | ->select('t.id, t.label, t.slug, count(e.id) as nbEntries') | |
76 | ->distinct(true) | |
77 | ->leftJoin('t.entries', 'e') | |
78 | ->where('e.user = :userId') | |
79 | ->groupBy('t.id') | |
80 | ->orderBy('t.slug') | |
81 | ->setParameter('userId', $userId) | |
82 | ->getQuery() | |
83 | ->getArrayResult(); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Used only in test case to get a tag for our entry. | |
88 | * | |
89 | * @return Tag | |
90 | */ | |
91 | public function findOneByEntryAndTagLabel($entry, $label) | |
92 | { | |
93 | return $this->createQueryBuilder('t') | |
94 | ->leftJoin('t.entries', 'e') | |
95 | ->where('e.id = :entryId')->setParameter('entryId', $entry->getId()) | |
96 | ->andWhere('t.label = :label')->setParameter('label', $label) | |
97 | ->setMaxResults(1) | |
98 | ->getQuery() | |
99 | ->getSingleResult(); | |
100 | } | |
101 | ||
102 | public function findForArchivedArticlesByUser($userId) | |
103 | { | |
104 | $ids = $this->createQueryBuilder('t') | |
105 | ->select('t.id') | |
106 | ->leftJoin('t.entries', 'e') | |
107 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
108 | ->andWhere('e.isArchived = true') | |
109 | ->groupBy('t.id') | |
110 | ->orderBy('t.slug') | |
111 | ->getQuery() | |
112 | ->getArrayResult(); | |
113 | ||
114 | $tags = []; | |
115 | foreach ($ids as $id) { | |
116 | $tags[] = $this->find($id); | |
117 | } | |
118 | ||
119 | return $tags; | |
120 | } | |
121 | } |