Skip to content

Instantly share code, notes, and snippets.

@baptistedonaux
Created February 19, 2015 11:32
Show Gist options
  • Save baptistedonaux/1cf86a18424abb18fe0d to your computer and use it in GitHub Desktop.
Save baptistedonaux/1cf86a18424abb18fe0d to your computer and use it in GitHub Desktop.
Soft Delete Symfony/Doctrine
# app/config/config.yml
doctrine:
orm:
filters:
deleted:
class: 'Namespace\MyBundle\Repository\Filters\DeletedFilter'
enabled: true
<?php
namespace Namespace\MyBundle\Repository\Filters;
use Doctrine\ORM\Mapping\ClassMetaData;
use Doctrine\ORM\Query\Filter\SQLFilter;
class DeletedFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
if ($targetEntity->hasField("deletedAt")) {
$date = date("Y-m-d h:m:s");
return $targetTableAlias.".deletedAt < '".$date."' OR ".$targetTableAlias.".deletedAt IS NULL";
}
return "";
}
}
<?php
namespace Namespace\MyBundle\Listener;
use Doctrine\ORM\Event\PreFlushEventArgs;
class DoctrineListener
{
public function preFlush(PreFlushEventArgs $event) {
$em = $event->getEntityManager();
foreach ($em->getUnitOfWork()->getScheduledEntityDeletions() as $object) {
if (method_exists($object, "getDeletedAt")) {
if ($object->getDeletedAt() instanceof \Datetime) {
continue;
} else {
$object->setDeletedAt(new \DateTime());
$em->merge($object);
$em->persist($object);
}
}
}
}
}
<?php
namespace Namespace\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Entity
{
/**
* @var datetime $deletedAt
*
* @ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
/**
* Set deletedAt
*
* @param \DateTime $deletedAt
* @return Plan
*/
public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get deletedAt
*
* @return \DateTime
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
}
# src/namespace/mybundle/Resources/config/services.yml
services:
kernel.listener.doctrine:
class: Namespace\MyBundle\Listener\DoctrineListener
public: false
tags:
- { name: doctrine.event_listener, event: preFlush, method: preFlush }
@adam-wysocki
Copy link

You can replace $em->merge() with just $em->persist() and $em->flush(), works flawlessly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment