Skip to content

Instantly share code, notes, and snippets.

@djaney
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save djaney/65d2dd3a89f115c4e9e8 to your computer and use it in GitHub Desktop.

Select an option

Save djaney/65d2dd3a89f115c4e9e8 to your computer and use it in GitHub Desktop.
Symfony2 soft delete
services:
task.subscriber:
class: Arcanys\TrainingBundle\EventListener\TaskEventListener
tags:
- { name: doctrine.event_subscriber, connection: default }
<?php namespace Arcanys\TrainingBundle\EventListener;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
use Arcanys\TrainingBundle\Entity\Task;
class TaskEventListener implements EventSubscriber{
public function getSubscribedEvents()
{
return array(
Events::preRemove,
);
}
public function preRemove(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof Task) {
$em = $args->getEntityManager();
$entity->setDeleted(true);
$em->detach($entity);
$entity->setDeleted(true);
$em->merge($entity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment