3 namespace Wallabag\CoreBundle\Event\Subscriber
;
5 use Doctrine\Bundle\DoctrineBundle\Registry
;
6 use Doctrine\Common\EventSubscriber
;
7 use Doctrine\ORM\Event\LifecycleEventArgs
;
8 use Wallabag\CoreBundle\Entity\Entry
;
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 ...
15 * @see https://www.sqlite.org/foreignkeys.html#fk_enable
17 class SQLiteCascadeDeleteSubscriber
implements EventSubscriber
22 * @param \Doctrine\Bundle\DoctrineBundle\Registry $doctrine
24 public function __construct(Registry
$doctrine)
26 $this->doctrine
= $doctrine;
32 public function getSubscribedEvents()
40 * We removed everything related to the upcoming removed entry because SQLite can't handle it on it own.
41 * We do it in the preRemove, because we can't retrieve tags in the postRemove (because the entry id is gone).
43 * @param LifecycleEventArgs $args
45 public function preRemove(LifecycleEventArgs
$args)
47 $entity = $args->getEntity();
48 if (!$this->doctrine
->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
49 || !$entity instanceof Entry
) {
53 $em = $this->doctrine
->getManager();
55 if (null !== $entity->getTags()) {
56 foreach ($entity->getTags() as $tag) {
57 $entity->removeTag($tag);
61 if (null !== $entity->getAnnotations()) {
62 foreach ($entity->getAnnotations() as $annotation) {
63 $em->remove($annotation);