diff options
author | Nicolas Lœuillet <nicolas@loeuillet.org> | 2016-01-02 12:46:52 +0100 |
---|---|---|
committer | Nicolas Lœuillet <nicolas@loeuillet.org> | 2016-01-02 12:46:52 +0100 |
commit | dad1c546a521159ca65a5a7649651d37728f0e55 (patch) | |
tree | f1d59eb83b45adb83bb21726374133a399d3c740 /src/Wallabag/CoreBundle/Repository | |
parent | 5432f6150939af6e7d2e8bf0faea0576491aaed0 (diff) | |
parent | 6be9750155fa731d75898b4973a126a090345c2d (diff) | |
download | wallabag-dad1c546a521159ca65a5a7649651d37728f0e55.tar.gz wallabag-dad1c546a521159ca65a5a7649651d37728f0e55.tar.zst wallabag-dad1c546a521159ca65a5a7649651d37728f0e55.zip |
Merge pull request #1545 from wallabag/v2-user-tag
v2 – Remove user reference in tag
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository')
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 44 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/TagRepository.php | 36 |
2 files changed, 67 insertions, 13 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 57bf8024..ca71970b 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -5,6 +5,7 @@ namespace Wallabag\CoreBundle\Repository; | |||
5 | use Doctrine\ORM\EntityRepository; | 5 | use Doctrine\ORM\EntityRepository; |
6 | use Pagerfanta\Adapter\DoctrineORMAdapter; | 6 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
7 | use Pagerfanta\Pagerfanta; | 7 | use Pagerfanta\Pagerfanta; |
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
8 | 9 | ||
9 | class EntryRepository extends EntityRepository | 10 | class EntryRepository extends EntityRepository |
10 | { | 11 | { |
@@ -179,4 +180,47 @@ class EntryRepository extends EntityRepository | |||
179 | ->getQuery() | 180 | ->getQuery() |
180 | ->getSingleResult(); | 181 | ->getSingleResult(); |
181 | } | 182 | } |
183 | |||
184 | /** | ||
185 | * Remove a tag from all user entries. | ||
186 | * | ||
187 | * 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. | ||
188 | * 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: | ||
189 | * | ||
190 | * 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 | ||
191 | * | ||
192 | * @param int $userId | ||
193 | * @param Tag $tag | ||
194 | */ | ||
195 | public function removeTag($userId, Tag $tag) | ||
196 | { | ||
197 | $entries = $this->getBuilderByUser($userId) | ||
198 | ->innerJoin('e.tags', 't') | ||
199 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | ||
200 | ->getQuery() | ||
201 | ->getResult(); | ||
202 | |||
203 | foreach ($entries as $entry) { | ||
204 | $entry->removeTag($tag); | ||
205 | } | ||
206 | |||
207 | $this->getEntityManager()->flush(); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * Find all entries that are attached to a give tag id. | ||
212 | * | ||
213 | * @param int $userId | ||
214 | * @param int $tagId | ||
215 | * | ||
216 | * @return array | ||
217 | */ | ||
218 | public function findAllByTagId($userId, $tagId) | ||
219 | { | ||
220 | return $this->getBuilderByUser($userId) | ||
221 | ->innerJoin('e.tags', 't') | ||
222 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | ||
223 | ->getQuery() | ||
224 | ->getResult(); | ||
225 | } | ||
182 | } | 226 | } |
diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index ac3145a1..c4aeb594 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php | |||
@@ -9,16 +9,29 @@ use Pagerfanta\Pagerfanta; | |||
9 | class TagRepository extends EntityRepository | 9 | class TagRepository extends EntityRepository |
10 | { | 10 | { |
11 | /** | 11 | /** |
12 | * Find Tags. | 12 | * Return only the QueryBuilder to retrieve all tags for a given user. |
13 | * | 13 | * |
14 | * @param int $userId | 14 | * @param int $userId |
15 | * | 15 | * |
16 | * @return array | 16 | * @return QueryBuilder |
17 | */ | ||
18 | private function getQbForAllTags($userId) | ||
19 | { | ||
20 | return $this->createQueryBuilder('t') | ||
21 | ->leftJoin('t.entries', 'e') | ||
22 | ->where('e.user = :userId')->setParameter('userId', $userId); | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * Find Tags and return a Pager. | ||
27 | * | ||
28 | * @param int $userId | ||
29 | * | ||
30 | * @return Pagerfanta | ||
17 | */ | 31 | */ |
18 | public function findTags($userId) | 32 | public function findTags($userId) |
19 | { | 33 | { |
20 | $qb = $this->createQueryBuilder('t') | 34 | $qb = $this->getQbForAllTags($userId); |
21 | ->where('t.user =:userId')->setParameter('userId', $userId); | ||
22 | 35 | ||
23 | $pagerAdapter = new DoctrineORMAdapter($qb); | 36 | $pagerAdapter = new DoctrineORMAdapter($qb); |
24 | 37 | ||
@@ -26,19 +39,16 @@ class TagRepository extends EntityRepository | |||
26 | } | 39 | } |
27 | 40 | ||
28 | /** | 41 | /** |
29 | * Find a tag by its label and its owner. | 42 | * Find Tags. |
30 | * | 43 | * |
31 | * @param string $label | 44 | * @param int $userId |
32 | * @param int $userId | ||
33 | * | 45 | * |
34 | * @return Tag|null | 46 | * @return array |
35 | */ | 47 | */ |
36 | public function findOneByLabelAndUserId($label, $userId) | 48 | public function findAllTags($userId) |
37 | { | 49 | { |
38 | return $this->createQueryBuilder('t') | 50 | return $this->getQbForAllTags($userId) |
39 | ->where('t.label = :label')->setParameter('label', $label) | ||
40 | ->andWhere('t.user = :user_id')->setParameter('user_id', $userId) | ||
41 | ->getQuery() | 51 | ->getQuery() |
42 | ->getOneOrNullResult(); | 52 | ->getResult(); |
43 | } | 53 | } |
44 | } | 54 | } |