]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/TagRepository.php
php-cs-fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / TagRepository.php
CommitLineData
b3dc0749
NL
1<?php
2
3namespace Wallabag\CoreBundle\Repository;
4
5use Doctrine\ORM\EntityRepository;
52b84c11 6use Wallabag\CoreBundle\Entity\Tag;
b3dc0749 7
6d37a7e6 8class 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
2a1ceb67 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
935e9fff
NH
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
567421af
TC
86 /**
87 * Used only in test case to get a tag for our entry.
88 *
89 * @return Tag
90 */
e686a76d 91 public function findOneByEntryAndTagLabel($entry, $label)
567421af
TC
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 }
6da1aebc 101
13a592a1 102 public function findForArchivedArticlesByUser($userId)
6da1aebc
TC
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 }
b3dc0749 121}