]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/TagRepository.php
Add ability to reset some datas
[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 *
38 * @param int $userId
39 *
40 * @return array
41 */
42 public function findAllTags($userId)
43 {
44 return $this->createQueryBuilder('t')
45 ->select('t.slug', 't.label', 't.id')
46 ->leftJoin('t.entries', 'e')
47 ->where('e.user = :userId')->setParameter('userId', $userId)
48 ->groupBy('t.slug')
49 ->addGroupBy('t.label')
50 ->addGroupBy('t.id')
51 ->getQuery()
52 ->getArrayResult();
53 }
54
55 /**
56 * Find all tags.
57 *
58 * @param int $userId
59 *
60 * @return array
61 */
62 public function findAllTags($userId)
63 {
64 return $this->createQueryBuilder('t')
65 ->select('t')
66 ->leftJoin('t.entries', 'e')
67 ->where('e.user = :userId')->setParameter('userId', $userId)
68 ->getQuery()
69 ->getResult();
70 }
71
72 /**
73 * Used only in test case to get a tag for our entry.
74 *
75 * @return Tag
76 */
77 public function findOneByEntryAndTagLabel($entry, $label)
78 {
79 return $this->createQueryBuilder('t')
80 ->leftJoin('t.entries', 'e')
81 ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
82 ->andWhere('t.label = :label')->setParameter('label', $label)
83 ->setMaxResults(1)
84 ->getQuery()
85 ->getSingleResult();
86 }
87 }