use Doctrine\ORM\EntityRepository;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
+use Wallabag\CoreBundle\Entity\Tag;
class EntryRepository extends EntityRepository
{
->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();
+ }
+
+ /**
+ * 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)
+ ->getQuery()
+ ->getResult();
+ }
}