3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Pagerfanta\Adapter\DoctrineORMAdapter
;
7 use Pagerfanta\Pagerfanta
;
8 use Wallabag\CoreBundle\Entity\Tag
;
10 class EntryRepository
extends EntityRepository
13 * Return a query builder to used by other getBuilderFor* method.
17 * @return QueryBuilder
19 private function getBuilderByUser($userId)
21 return $this->createQueryBuilder('e')
22 ->leftJoin('e.user', 'u')
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.id', 'desc')
29 * Retrieves all entries for a user.
33 * @return QueryBuilder
35 public function getBuilderForAllByUser($userId)
38 ->getBuilderByUser($userId)
43 * Retrieves unread entries for a user.
47 * @return QueryBuilder
49 public function getBuilderForUnreadByUser($userId)
52 ->getBuilderByUser($userId)
53 ->andWhere('e.isArchived = false')
58 * Retrieves read entries for a user.
62 * @return QueryBuilder
64 public function getBuilderForArchiveByUser($userId)
67 ->getBuilderByUser($userId)
68 ->andWhere('e.isArchived = true')
73 * Retrieves starred entries for a user.
77 * @return QueryBuilder
79 public function getBuilderForStarredByUser($userId)
82 ->getBuilderByUser($userId)
83 ->andWhere('e.isStarred = true')
91 * @param bool $isArchived
92 * @param bool $isStarred
94 * @param string $order
98 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
100 $qb = $this->createQueryBuilder('e')
101 ->where('e.user =:userId')->setParameter('userId', $userId);
103 if (null !== $isArchived) {
104 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
107 if (null !== $isStarred) {
108 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
111 if ('created' === $sort) {
112 $qb->orderBy('e.id', $order);
113 } elseif ('updated' === $sort) {
114 $qb->orderBy('e.updatedAt', $order);
117 $pagerAdapter = new DoctrineORMAdapter($qb);
119 return new Pagerfanta($pagerAdapter);
123 * Fetch an entry with a tag. Only used for tests.
129 public function findOneWithTags($userId)
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)
138 return $qb->getQuery()->getResult();
142 * Find distinct language for a given user.
143 * Used to build the filter language list.
145 * @param int $userId User id
149 public function findDistinctLanguageByUser($userId)
151 $results = $this->createQueryBuilder('e')
152 ->select('e.language')
153 ->where('e.user = :userId')->setParameter('userId', $userId)
154 ->andWhere('e.language IS NOT NULL')
155 ->groupBy('e.language')
156 ->orderBy('e.language', ' ASC')
160 $languages = array();
161 foreach ($results as $result) {
162 $languages[$result['language']] = $result['language'];
169 * Used only in test case to get the right entry associated to the right user.
171 * @param string $username
175 public function findOneByUsernameAndNotArchived($username)
177 return $this->createQueryBuilder('e')
178 ->leftJoin('e.user', 'u')
179 ->where('u.username = :username')->setParameter('username', $username)
180 ->andWhere('e.isArchived = false')
187 * Remove a tag from all user entries.
189 * We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation.
190 * It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix:
192 * DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId
197 public function removeTag($userId, Tag
$tag)
199 $entries = $this->getBuilderByUser($userId)
200 ->innerJoin('e.tags', 't')
201 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
205 foreach ($entries as $entry) {
206 $entry->removeTag($tag);
209 $this->getEntityManager()->flush();
213 * Find all entries that are attached to a give tag id.
220 public function findAllByTagId($userId, $tagId)
222 return $this->getBuilderByUser($userId)
223 ->innerJoin('e.tags', 't')
224 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
230 * Find an entry by its url and its owner.
231 * If it exists, return the entry otherwise return false.
238 public function findByUrlAndUserId($url, $userId)
240 $res = $this->createQueryBuilder('e')
241 ->where('e.url = :url')->setParameter('url', $url)
242 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
247 return current($res);
254 * Count all entries for a user.
260 public function countAllEntriesByUsername($userId)
262 $qb = $this->createQueryBuilder('e')
264 ->where('e.user=:userId')->setParameter('userId', $userId)
267 return $qb->getQuery()->getSingleScalarResult();