3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Pagerfanta\Adapter\DoctrineORMAdapter
;
7 use Pagerfanta\Pagerfanta
;
9 class EntryRepository
extends EntityRepository
12 * Return a query builder to used by other getBuilderFor* method.
16 * @return QueryBuilder
18 private function getBuilderByUser($userId)
20 return $this->createQueryBuilder('e')
21 ->leftJoin('e.user', 'u')
22 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
23 ->orderBy('e.id', 'desc')
28 * Retrieves all entries for a user.
32 * @return QueryBuilder
34 public function getBuilderForAllByUser($userId)
37 ->getBuilderByUser($userId)
42 * Retrieves unread entries for a user.
46 * @return QueryBuilder
48 public function getBuilderForUnreadByUser($userId)
51 ->getBuilderByUser($userId)
52 ->andWhere('e.isArchived = false')
57 * Retrieves read entries for a user.
61 * @return QueryBuilder
63 public function getBuilderForArchiveByUser($userId)
66 ->getBuilderByUser($userId)
67 ->andWhere('e.isArchived = true')
72 * Retrieves starred entries for a user.
76 * @return QueryBuilder
78 public function getBuilderForStarredByUser($userId)
81 ->getBuilderByUser($userId)
82 ->andWhere('e.isStarred = true')
90 * @param bool $isArchived
91 * @param bool $isStarred
93 * @param string $order
97 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
99 $qb = $this->createQueryBuilder('e')
100 ->where('e.user =:userId')->setParameter('userId', $userId);
102 if (null !== $isArchived) {
103 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
106 if (null !== $isStarred) {
107 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
110 if ('created' === $sort) {
111 $qb->orderBy('e.id', $order);
112 } elseif ('updated' === $sort) {
113 $qb->orderBy('e.updatedAt', $order);
116 $pagerAdapter = new DoctrineORMAdapter($qb);
118 return new Pagerfanta($pagerAdapter);
122 * Fetch an entry with a tag. Only used for tests.
126 public function findOneWithTags($userId)
128 $qb = $this->createQueryBuilder('e')
129 ->innerJoin('e.tags', 't')
130 ->innerJoin('e.user', 'u')
131 ->addSelect('t', 'u')
132 ->where('e.user=:userId')->setParameter('userId', $userId)
135 return $qb->getQuery()->getResult();
139 * Find distinct language for a given user.
140 * Used to build the filter language list.
142 * @param int $userId User id
146 public function findDistinctLanguageByUser($userId)
148 $results = $this->createQueryBuilder('e')
149 ->select('e.language')
150 ->where('e.user = :userId')->setParameter('userId', $userId)
151 ->andWhere('e.language IS NOT NULL')
152 ->groupBy('e.language')
153 ->orderBy('e.language', ' ASC')
157 $languages = array();
158 foreach ($results as $result) {
159 $languages[$result['language']] = $result['language'];
166 * Used only in test case to get the right entry associated to the right user.
168 * @param string $username
172 public function findOneByUsernameAndNotArchived($username)
174 return $this->createQueryBuilder('e')
175 ->leftJoin('e.user', 'u')
176 ->where('u.username = :username')->setParameter('username', $username)
177 ->andWhere('e.isArchived = false')