3 namespace Wallabag\AnnotationBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Doctrine\ORM\QueryBuilder
;
7 use Wallabag\AnnotationBundle\Entity\Annotation
;
10 * AnnotationRepository.
12 class AnnotationRepository
extends EntityRepository
15 * Retrieves all annotations for a user.
19 * @return QueryBuilder
21 public function getBuilderForAllByUser($userId)
24 ->getSortedQueryBuilderByUser($userId)
29 * Get annotation for this id.
31 * @param int $annotationId
35 public function findAnnotationById($annotationId)
37 return $this->createQueryBuilder('a')
38 ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId)
45 * Find annotations for entry id.
52 public function findAnnotationsByPageId($entryId, $userId)
54 return $this->createQueryBuilder('a')
55 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
56 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
63 * Find last annotation for a given entry id. Used only for tests.
69 public function findLastAnnotationByPageId($entryId, $userId)
71 return $this->createQueryBuilder('a')
72 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
73 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
74 ->orderBy('a.id', 'DESC')
77 ->getOneOrNullResult();
81 * Used only in test case to get the right annotation associated to the right user.
83 * @param string $username
87 public function findOneByUsername($username)
89 return $this->createQueryBuilder('a')
90 ->leftJoin('a.user', 'u')
91 ->where('u.username = :username')->setParameter('username', $username)
92 ->orderBy('a.id', 'DESC')
99 * Remove all annotations for a user id.
100 * Used when a user want to reset all informations.
104 public function removeAllByUserId($userId)
106 $this->getEntityManager()
107 ->createQuery('DELETE FROM Wallabag\AnnotationBundle\Entity\Annotation a WHERE a.user = :userId')
108 ->setParameter('userId', $userId)
113 * Find all annotations related to archived entries.
119 public function findAllArchivedEntriesByUser($userId)
121 return $this->createQueryBuilder('a')
122 ->leftJoin('a.entry', 'e')
123 ->where('a.user = :userid')->setParameter(':userid', $userId)
124 ->andWhere('e.isArchived = true')
130 * Return a query builder to used by other getBuilderFor* method.
134 * @return QueryBuilder
136 private function getSortedQueryBuilderByUser($userId)
138 return $this->createQueryBuilder('a')
139 ->leftJoin('a.user', 'u')
140 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
141 ->orderBy('a.id', 'desc')