]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Sort by id is faster
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Doctrine\ORM\Tools\Pagination\Paginator;
7 use Pagerfanta\Adapter\DoctrineORMAdapter;
8 use Pagerfanta\Pagerfanta;
9
10 class EntryRepository extends EntityRepository
11 {
12 /**
13 * Retrieves unread entries for a user
14 *
15 * @param int $userId
16 * @param int $firstResult
17 * @param int $maxResults
18 *
19 * @return Paginator
20 */
21 public function findUnreadByUser($userId, $firstResult, $maxResults = 12)
22 {
23 $qb = $this->createQueryBuilder('e')
24 ->setFirstResult($firstResult)
25 ->setMaxResults($maxResults)
26 ->leftJoin('e.user', 'u')
27 ->where('e.isArchived = false')
28 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
29 ->orderBy('e.id', 'desc')
30 ->getQuery();
31
32 $paginator = new Paginator($qb);
33
34 return $paginator;
35 }
36
37 /**
38 * Retrieves read entries for a user
39 *
40 * @param int $userId
41 * @param int $firstResult
42 * @param int $maxResults
43 *
44 * @return Paginator
45 */
46 public function findArchiveByUser($userId, $firstResult, $maxResults = 12)
47 {
48 $qb = $this->createQueryBuilder('e')
49 ->select('e')
50 ->setFirstResult($firstResult)
51 ->setMaxResults($maxResults)
52 ->leftJoin('e.user', 'u')
53 ->where('e.isArchived = true')
54 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
55 ->orderBy('e.id', 'desc')
56 ->getQuery();
57
58 $paginator = new Paginator($qb);
59
60 return $paginator;
61 }
62
63 /**
64 * Retrieves starred entries for a user
65 *
66 * @param int $userId
67 * @param int $firstResult
68 * @param int $maxResults
69 *
70 * @return Paginator
71 */
72 public function findStarredByUser($userId, $firstResult, $maxResults = 12)
73 {
74 $qb = $this->createQueryBuilder('e')
75 ->select('e')
76 ->setFirstResult($firstResult)
77 ->setMaxResults($maxResults)
78 ->leftJoin('e.user', 'u')
79 ->where('e.isStarred = true')
80 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
81 ->orderBy('e.id', 'desc')
82 ->getQuery();
83
84 $paginator = new Paginator($qb);
85
86 return $paginator;
87 }
88
89 /**
90 * Find Entries
91 *
92 * @param int $userId
93 * @param bool $isArchived
94 * @param bool $isStarred
95 * @param string $sort
96 * @param string $order
97 *
98 * @return array
99 */
100 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
101 {
102 $qb = $this->createQueryBuilder('e')
103 ->where('e.user =:userId')->setParameter('userId', $userId);
104
105 if (null !== $isArchived) {
106 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
107 }
108
109 if (null !== $isStarred) {
110 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
111 }
112
113 if ('created' === $sort) {
114 $qb->orderBy('e.id', $order);
115 } elseif ('updated' === $sort) {
116 $qb->orderBy('e.updatedAt', $order);
117 }
118
119 $pagerAdapter = new DoctrineORMAdapter($qb);
120
121 return new Pagerfanta($pagerAdapter);
122 }
123
124 /**
125 * Fetch an entry with a tag. Only used for tests.
126 *
127 * @return Entry
128 */
129 public function findOneWithTags($userId)
130 {
131 $qb = $this->createQueryBuilder('e')
132 ->innerJoin('e.tags', 't')
133 ->innerJoin('e.user', 'u')
134 ->addSelect('t', 'u')
135 ->where('e.user=:userId')->setParameter('userId', $userId)
136 ;
137
138 return $qb->getQuery()->getResult();
139 }
140 }