+ $pagerAdapter = new DoctrineORMAdapter($qb);
+
+ return new Pagerfanta($pagerAdapter);
+ }
+
+ /**
+ * Fetch an entry with a tag. Only used for tests.
+ *
+ * @param int $userId
+ *
+ * @return Entry
+ */
+ public function findOneWithTags($userId)
+ {
+ $qb = $this->createQueryBuilder('e')
+ ->innerJoin('e.tags', 't')
+ ->innerJoin('e.user', 'u')
+ ->addSelect('t', 'u')
+ ->where('e.user=:userId')->setParameter('userId', $userId)
+ ;
+
+ return $qb->getQuery()->getResult();
+ }
+
+ /**
+ * Find distinct language for a given user.
+ * Used to build the filter language list.
+ *
+ * @param int $userId User id
+ *
+ * @return array
+ */
+ public function findDistinctLanguageByUser($userId)
+ {
+ $results = $this->createQueryBuilder('e')
+ ->select('e.language')
+ ->where('e.user = :userId')->setParameter('userId', $userId)
+ ->andWhere('e.language IS NOT NULL')
+ ->groupBy('e.language')
+ ->orderBy('e.language', ' ASC')
+ ->getQuery()
+ ->getResult();
+
+ $languages = [];
+ foreach ($results as $result) {
+ $languages[$result['language']] = $result['language'];
+ }
+
+ return $languages;
+ }
+
+ /**
+ * Used only in test case to get the right entry associated to the right user.
+ *
+ * @param string $username
+ *
+ * @return Entry
+ */
+ public function findOneByUsernameAndNotArchived($username)
+ {
+ return $this->createQueryBuilder('e')
+ ->leftJoin('e.user', 'u')
+ ->where('u.username = :username')->setParameter('username', $username)
+ ->andWhere('e.isArchived = false')
+ ->setMaxResults(1)
+ ->getQuery()
+ ->getSingleResult();
+ }
+
+ /**
+ * Remove a tag from all user entries.
+ *
+ * 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.
+ * 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:
+ *
+ * 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
+ *
+ * @param int $userId
+ * @param Tag $tag
+ */
+ public function removeTag($userId, Tag $tag)
+ {
+ $entries = $this->getBuilderByUser($userId)
+ ->innerJoin('e.tags', 't')
+ ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
+ ->getQuery()
+ ->getResult();
+
+ foreach ($entries as $entry) {
+ $entry->removeTag($tag);
+ }
+
+ $this->getEntityManager()->flush();
+ }
+
+ /**
+ * Remove tags from all user entries.
+ *
+ * @param int $userId
+ * @param Array<Tag> $tags
+ */
+ public function removeTags($userId, $tags)
+ {
+ foreach ($tags as $tag) {
+ $this->removeTag($userId, $tag);
+ }
+ }
+
+ /**
+ * Find all entries that are attached to a give tag id.
+ *
+ * @param int $userId
+ * @param int $tagId
+ *
+ * @return array
+ */
+ public function findAllByTagId($userId, $tagId)
+ {
+ return $this->getBuilderByUser($userId)
+ ->innerJoin('e.tags', 't')
+ ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)