1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-10-15 19:42:08 +00:00

Remove user reference in tag

Fix #1543
This commit is contained in:
Jeremy Benoist 2015-12-29 14:50:52 +01:00
parent c997cfcc9c
commit fc73222723
13 changed files with 123 additions and 99 deletions

View file

@ -5,6 +5,7 @@ namespace Wallabag\CoreBundle\Repository;
use Doctrine\ORM\EntityRepository;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Wallabag\CoreBundle\Entity\Tag;
class EntryRepository extends EntityRepository
{
@ -179,4 +180,22 @@ class EntryRepository extends EntityRepository
->getQuery()
->getSingleResult();
}
/**
* Remove a tag from all user entries.
* We are using a native SQL query because Doctrine doesn't know EntryTag entity because it's a ManyToMany relation.
* Instead of that SQL query we should loop on every entry and remove the tag, could be really long ...
*
* @param int $userId
* @param Tag $tag
*/
public function removeTag($userId, Tag $tag)
{
$sql = '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';
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute([
'userId' => $userId,
'tagId' => $tag->getId(),
]);
}
}

View file

@ -9,16 +9,29 @@ use Pagerfanta\Pagerfanta;
class TagRepository extends EntityRepository
{
/**
* Find Tags.
* Return only the QueryBuilder to retrieve all tags for a given user.
*
* @param int $userId
*
* @return array
* @return QueryBuilder
*/
private function getQbForAllTags($userId)
{
return $this->createQueryBuilder('t')
->leftJoin('t.entries', 'e')
->where('e.user = :userId')->setParameter('userId', $userId);
}
/**
* Find Tags and return a Pager.
*
* @param int $userId
*
* @return Pagerfanta
*/
public function findTags($userId)
{
$qb = $this->createQueryBuilder('t')
->where('t.user =:userId')->setParameter('userId', $userId);
$qb = $this->getQbForAllTags($userId);
$pagerAdapter = new DoctrineORMAdapter($qb);
@ -26,19 +39,16 @@ class TagRepository extends EntityRepository
}
/**
* Find a tag by its label and its owner.
* Find Tags.
*
* @param string $label
* @param int $userId
* @param int $userId
*
* @return Tag|null
* @return array
*/
public function findOneByLabelAndUserId($label, $userId)
public function findAllTags($userId)
{
return $this->createQueryBuilder('t')
->where('t.label = :label')->setParameter('label', $label)
->andWhere('t.user = :user_id')->setParameter('user_id', $userId)
return $this->getQbForAllTags($userId)
->getQuery()
->getOneOrNullResult();
->getResult();
}
}