1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-15 18:57:05 +00:00

Add custom doctrine subscriber for SQLite

Since SQLite doesn’t handle cascade remove by default, we need to handle it manually.

Also some refacto
This commit is contained in:
Jeremy Benoist 2016-10-01 14:01:13 +02:00
parent 98efffc2a6
commit 191564b7f7
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
5 changed files with 144 additions and 11 deletions

View file

@ -0,0 +1,70 @@
<?php
namespace Wallabag\CoreBundle\Subscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Wallabag\CoreBundle\Entity\Entry;
use Doctrine\Bundle\DoctrineBundle\Registry;
/**
* SQLite doesn't care about cascading remove, so we need to manually remove associated stuf for an Entry.
* Foreign Key Support can be enabled by running `PRAGMA foreign_keys = ON;` at runtime (AT RUNTIME !).
* But it needs a compilation flag that not all SQLite instance has ...
*
* @see https://www.sqlite.org/foreignkeys.html#fk_enable
*/
class SQLiteCascadeDeleteSubscriber implements EventSubscriber
{
private $doctrine;
/**
* @param \Doctrine\Bundle\DoctrineBundle\Registry $doctrine
*/
public function __construct(Registry $doctrine)
{
$this->doctrine = $doctrine;
}
/**
* @return array
*/
public function getSubscribedEvents()
{
return [
'preRemove',
];
}
/**
* We removed everything related to the upcoming removed entry because SQLite can't handle it on it own.
* We do it in the preRemove, because we can't retrieve tags in the postRemove (because the entry id is gone)
*
* @param LifecycleEventArgs $args
*/
public function preRemove(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if (!$this->doctrine->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver ||
!$entity instanceof Entry) {
return;
}
$em = $this->doctrine->getManager();
if (null !== $entity->getTags()) {
foreach ($entity->getTags() as $tag) {
$entity->removeTag($tag);
}
}
if (null !== $entity->getAnnotations()) {
foreach ($entity->getAnnotations() as $annotation) {
$em->remove($annotation);
}
}
$em->flush();
}
}