3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Doctrine\ORM\Query
;
7 use Pagerfanta\Adapter\DoctrineORMAdapter
;
8 use Pagerfanta\Pagerfanta
;
9 use Wallabag\CoreBundle\Entity\Tag
;
11 class EntryRepository
extends EntityRepository
14 * Return a query builder to used by other getBuilderFor* method.
18 * @return QueryBuilder
20 private function getBuilderByUser($userId)
22 return $this->createQueryBuilder('e')
23 ->leftJoin('e.user', 'u')
24 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
25 ->orderBy('e.id', 'desc')
30 * Retrieves all entries for a user.
34 * @return QueryBuilder
36 public function getBuilderForAllByUser($userId)
39 ->getBuilderByUser($userId)
44 * Retrieves unread entries for a user.
48 * @return QueryBuilder
50 public function getBuilderForUnreadByUser($userId)
53 ->getBuilderByUser($userId)
54 ->andWhere('e.isArchived = false')
59 * Retrieves read entries for a user.
63 * @return QueryBuilder
65 public function getBuilderForArchiveByUser($userId)
68 ->getBuilderByUser($userId)
69 ->andWhere('e.isArchived = true')
74 * Retrieves starred entries for a user.
78 * @return QueryBuilder
80 public function getBuilderForStarredByUser($userId)
83 ->getBuilderByUser($userId)
84 ->andWhere('e.isStarred = true')
92 * @param bool $isArchived
93 * @param bool $isStarred
95 * @param string $order
101 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
103 $qb = $this->createQueryBuilder('e')
104 ->leftJoin('e.tags', 't')
105 ->where('e.user =:userId')->setParameter('userId', $userId);
107 if (null !== $isArchived) {
108 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
111 if (null !== $isStarred) {
112 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
116 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
120 foreach (explode(',', $tags) as $tag) {
121 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
125 if ('created' === $sort) {
126 $qb->orderBy('e.id', $order);
127 } elseif ('updated' === $sort) {
128 $qb->orderBy('e.updatedAt', $order);
131 $pagerAdapter = new DoctrineORMAdapter($qb);
133 return new Pagerfanta($pagerAdapter);
137 * Fetch an entry with a tag. Only used for tests.
143 public function findOneWithTags($userId)
145 $qb = $this->createQueryBuilder('e')
146 ->innerJoin('e.tags', 't')
147 ->innerJoin('e.user', 'u')
148 ->addSelect('t', 'u')
149 ->where('e.user=:userId')->setParameter('userId', $userId)
152 return $qb->getQuery()->getResult();
156 * Find distinct language for a given user.
157 * Used to build the filter language list.
159 * @param int $userId User id
163 public function findDistinctLanguageByUser($userId)
165 $results = $this->createQueryBuilder('e')
166 ->select('e.language')
167 ->where('e.user = :userId')->setParameter('userId', $userId)
168 ->andWhere('e.language IS NOT NULL')
169 ->groupBy('e.language')
170 ->orderBy('e.language', ' ASC')
175 foreach ($results as $result) {
176 $languages[$result['language']] = $result['language'];
183 * Used only in test case to get the right entry associated to the right user.
185 * @param string $username
189 public function findOneByUsernameAndNotArchived($username)
191 return $this->createQueryBuilder('e')
192 ->leftJoin('e.user', 'u')
193 ->where('u.username = :username')->setParameter('username', $username)
194 ->andWhere('e.isArchived = false')
201 * Remove a tag from all user entries.
203 * 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.
204 * 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:
206 * 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
211 public function removeTag($userId, Tag
$tag)
213 $entries = $this->getBuilderByUser($userId)
214 ->innerJoin('e.tags', 't')
215 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
219 foreach ($entries as $entry) {
220 $entry->removeTag($tag);
223 $this->getEntityManager()->flush();
227 * Find all entries that are attached to a give tag id.
234 public function findAllByTagId($userId, $tagId)
236 return $this->getBuilderByUser($userId)
237 ->innerJoin('e.tags', 't')
238 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
244 * Find an entry by its url and its owner.
245 * If it exists, return the entry otherwise return false.
252 public function findByUrlAndUserId($url, $userId)
254 $res = $this->createQueryBuilder('e')
255 ->where('e.url = :url')->setParameter('url', $url)
256 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
261 return current($res);
268 * Count all entries for a user.
274 public function countAllEntriesByUsername($userId)
276 $qb = $this->createQueryBuilder('e')
278 ->where('e.user=:userId')->setParameter('userId', $userId)
281 return $qb->getQuery()->getSingleScalarResult();
285 * Enable cache for a query
287 * @param Query $query
291 public function enableCache(Query
$query)
293 $query->useQueryCache(true);
294 $query->useResultCache(true);
295 $query->setResultCacheLifetime(5);