-
-
Save tonydub/ff1a34b9125b49aa4090772651efdac2 to your computer and use it in GitHub Desktop.
Soft Delete Symfony/Doctrine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/config/config.yml | |
doctrine: | |
orm: | |
filters: | |
deleted: | |
class: 'Namespace\MyBundle\Repository\Filters\DeletedFilter' | |
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ""; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment