]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php
Merge pull request #4151 from ldidry/fix-4060
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / SQLiteCascadeDeleteSubscriber.php
CommitLineData
191564b7 1<?php
8c61fd12 2
535bfcbe 3namespace Wallabag\CoreBundle\Event\Subscriber;
191564b7 4
f808b016 5use Doctrine\Bundle\DoctrineBundle\Registry;
191564b7 6use Doctrine\Common\EventSubscriber;
191564b7
JB
7use Doctrine\ORM\Event\LifecycleEventArgs;
8use Wallabag\CoreBundle\Entity\Entry;
191564b7
JB
9
10/**
11 * SQLite doesn't care about cascading remove, so we need to manually remove associated stuf for an Entry.
12 * Foreign Key Support can be enabled by running `PRAGMA foreign_keys = ON;` at runtime (AT RUNTIME !).
13 * But it needs a compilation flag that not all SQLite instance has ...
14 *
15 * @see https://www.sqlite.org/foreignkeys.html#fk_enable
16 */
17class SQLiteCascadeDeleteSubscriber implements EventSubscriber
18{
19 private $doctrine;
20
191564b7
JB
21 public function __construct(Registry $doctrine)
22 {
23 $this->doctrine = $doctrine;
24 }
25
26 /**
27 * @return array
28 */
29 public function getSubscribedEvents()
30 {
31 return [
32 'preRemove',
33 ];
34 }
35
36 /**
37 * We removed everything related to the upcoming removed entry because SQLite can't handle it on it own.
8c61fd12 38 * We do it in the preRemove, because we can't retrieve tags in the postRemove (because the entry id is gone).
191564b7
JB
39 */
40 public function preRemove(LifecycleEventArgs $args)
41 {
42 $entity = $args->getEntity();
7ab5eb95 43 if (!$this->doctrine->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
44 || !$entity instanceof Entry) {
191564b7
JB
45 return;
46 }
47
48 $em = $this->doctrine->getManager();
49
50 if (null !== $entity->getTags()) {
51 foreach ($entity->getTags() as $tag) {
52 $entity->removeTag($tag);
53 }
54 }
55
56 if (null !== $entity->getAnnotations()) {
57 foreach ($entity->getAnnotations() as $annotation) {
58 $em->remove($annotation);
59 }
60 }
61
62 $em->flush();
63 }
64}