aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-12-29 15:08:33 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2015-12-29 15:43:32 +0100
commit4059a061c0c8cc787f71e96aef2ab01599d3241d (patch)
tree44265f4985fe9ecf9b601d3414971a62bbc8034d /src
parent1bb1939ab76cfbf1cdb5fa1dccbdd15ba17cdfb0 (diff)
downloadwallabag-4059a061c0c8cc787f71e96aef2ab01599d3241d.tar.gz
wallabag-4059a061c0c8cc787f71e96aef2ab01599d3241d.tar.zst
wallabag-4059a061c0c8cc787f71e96aef2ab01599d3241d.zip
Fix the way to remove a tag from all user entries
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php7
-rw-r--r--src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php9
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php47
3 files changed, 53 insertions, 10 deletions
diff --git a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
index a7120e83..22894a77 100644
--- a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
+++ b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
@@ -310,5 +310,12 @@ class WallabagRestControllerTest extends WallabagApiTestCase
310 $this->assertArrayHasKey('label', $content); 310 $this->assertArrayHasKey('label', $content);
311 $this->assertEquals($tag['label'], $content['label']); 311 $this->assertEquals($tag['label'], $content['label']);
312 $this->assertEquals($tag['slug'], $content['slug']); 312 $this->assertEquals($tag['slug'], $content['slug']);
313
314 $entries = $entry = $this->client->getContainer()
315 ->get('doctrine.orm.entity_manager')
316 ->getRepository('WallabagCoreBundle:Entry')
317 ->findAllByTagId($this->user->getId(), $tag['id']);
318
319 $this->assertCount(0, $entries);
313 } 320 }
314} 321}
diff --git a/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php b/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php
index 8a57fea2..a415c749 100644
--- a/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php
+++ b/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php
@@ -12,6 +12,11 @@ abstract class WallabagApiTestCase extends WebTestCase
12 */ 12 */
13 protected $client = null; 13 protected $client = null;
14 14
15 /**
16 * @var \FOS\UserBundle\Model\UserInterface
17 */
18 protected $user;
19
15 public function setUp() 20 public function setUp()
16 { 21 {
17 $this->client = $this->createAuthorizedClient(); 22 $this->client = $this->createAuthorizedClient();
@@ -31,8 +36,8 @@ abstract class WallabagApiTestCase extends WebTestCase
31 $loginManager = $container->get('fos_user.security.login_manager'); 36 $loginManager = $container->get('fos_user.security.login_manager');
32 $firewallName = $container->getParameter('fos_user.firewall_name'); 37 $firewallName = $container->getParameter('fos_user.firewall_name');
33 38
34 $user = $userManager->findUserBy(array('username' => 'admin')); 39 $this->user = $userManager->findUserBy(array('username' => 'admin'));
35 $loginManager->loginUser($firewallName, $user); 40 $loginManager->loginUser($firewallName, $this->user);
36 41
37 // save the login token into the session and put it in a cookie 42 // save the login token into the session and put it in a cookie
38 $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); 43 $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 9ff80d6e..e658a359 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -183,19 +183,50 @@ class EntryRepository extends EntityRepository
183 183
184 /** 184 /**
185 * Remove a tag from all user entries. 185 * Remove a tag from all user entries.
186 * We are using a native SQL query because Doctrine doesn't know EntryTag entity because it's a ManyToMany relation. 186 *
187 * Instead of that SQL query we should loop on every entry and remove the tag, could be really long ... 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.
188 * 189 *
189 * @param int $userId 190 * @param int $userId
190 * @param Tag $tag 191 * @param Tag $tag
191 */ 192 */
192 public function removeTag($userId, Tag $tag) 193 public function removeTag($userId, Tag $tag)
193 { 194 {
194 $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'; 195 $entries = $this->getBuilderByUser($userId)
195 $stmt = $this->getEntityManager()->getConnection()->prepare($sql); 196 ->innerJoin('e.tags', 't')
196 $stmt->execute([ 197 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
197 'userId' => $userId, 198 ->getQuery()
198 'tagId' => $tag->getId(), 199 ->getResult();
199 ]); 200
201 foreach ($entries as $entry) {
202 $entry->removeTag($tag);
203 }
204
205 $this->getEntityManager()->flush();
206
207 // An other solution can be to use raw query but I can't find a way to retrieve the `entry_tag` table name since it can be prefixed....
208 // $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';
209 // $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
210 // $stmt->execute([
211 // 'userId' => $userId,
212 // 'tagId' => $tag->getId(),
213 // ]);
214 }
215
216 /**
217 * Find all entries that are attached to a give tag id.
218 *
219 * @param int $userId
220 * @param int $tagId
221 *
222 * @return array
223 */
224 public function findAllByTagId($userId, $tagId)
225 {
226 return $this->getBuilderByUser($userId)
227 ->innerJoin('e.tags', 't')
228 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
229 ->getQuery()
230 ->getResult();
200 } 231 }
201} 232}