]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/TagRepository.php
Merge pull request #2616 from mathieui/doc-https-links
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / TagRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6
7 class TagRepository extends EntityRepository
8 {
9 /**
10 * Count all tags per user.
11 *
12 * @param int $userId
13 * @param int $cacheLifeTime Duration of the cache for this query
14 *
15 * @return int
16 */
17 public function countAllTags($userId, $cacheLifeTime = null)
18 {
19 $query = $this->createQueryBuilder('t')
20 ->select('t.slug')
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
32 return count($query->getArrayResult());
33 }
34
35 /**
36 * Find all tags per user.
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.
40 *
41 * @param int $userId
42 *
43 * @return array
44 */
45 public function findAllTags($userId)
46 {
47 $ids = $this->createQueryBuilder('t')
48 ->select('t.id')
49 ->leftJoin('t.entries', 'e')
50 ->where('e.user = :userId')->setParameter('userId', $userId)
51 ->groupBy('t.id')
52 ->getQuery()
53 ->getArrayResult();
54
55 $tags = [];
56 foreach ($ids as $id) {
57 $tags[] = $this->find($id);
58 }
59
60 return $tags;
61 }
62
63 /**
64 * Used only in test case to get a tag for our entry.
65 *
66 * @return Tag
67 */
68 public function findOneByEntryAndTagLabel($entry, $label)
69 {
70 return $this->createQueryBuilder('t')
71 ->leftJoin('t.entries', 'e')
72 ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
73 ->andWhere('t.label = :label')->setParameter('label', $label)
74 ->setMaxResults(1)
75 ->getQuery()
76 ->getSingleResult();
77 }
78 }