forked from KnpLabs/DoctrineBehaviors
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
13 deletions.
There are no files selected for viewing
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
56 changes: 44 additions & 12 deletions
56
src/Knp/DoctrineBehaviors/ORM/SoftDeletable/SoftDeletableListener.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,76 @@ | ||
<?php | ||
|
||
namespace Knp\DoctrineBehaviors\ORM\SoftDeletable; | ||
/* | ||
* This file is part of the KnpDoctrineBehaviors package. | ||
* | ||
* (c) KnpLabs <http://knplabs.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Doctrine\Common\Persistence\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||
namespace Knp\DoctrineBehaviors\ORM\SoftDeletable; | ||
|
||
use Doctrine\Common\EventSubscriber, | ||
use Doctrine\Common\Persistence\Mapping\ClassMetadata, | ||
Doctrine\Common\EventSubscriber, | ||
Doctrine\ORM\Event\OnFlushEventArgs, | ||
Doctrine\ORM\Events; | ||
|
||
/** | ||
* SoftDeletable Doctrine2 listener. | ||
* | ||
* Listens to onFlush event and marks SoftDeletable entities | ||
* as deleted instead of really removing them. | ||
*/ | ||
class SoftDeletableListener implements EventSubscriber | ||
{ | ||
/** | ||
* Listens to onFlush event. | ||
* | ||
* @param OnFlushEventArgs $args The event arguments | ||
*/ | ||
public function onFlush(OnFlushEventArgs $args) | ||
{ | ||
$em = $args->getEntityManager(); | ||
$em = $args->getEntityManager(); | ||
$uow = $em->getUnitOfWork(); | ||
|
||
foreach ($uow->getScheduledEntityDeletions() as $entity) { | ||
$classMetadata = $em->getClassMetadata(get_class($entity)); | ||
if ($this->isEntitySupported($classMetadata)) { | ||
$oldValue = $entity->getDeletedAt(); | ||
|
||
$entity->delete(); | ||
$em->persist($entity); | ||
|
||
$uow->propertyChanged($entity, 'deletedAt', $oldValue, $entity->getDeletedAt()); | ||
$uow->scheduleExtraUpdate($entity, array( | ||
'deletedAt' => array($oldValue, $entity->getDeletedAt()) | ||
)); | ||
$uow->scheduleExtraUpdate($entity, [ | ||
'deletedAt' => [$oldValue, $entity->getDeletedAt()] | ||
]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether provided entity is supported. | ||
* | ||
* @param ClassMetadata $classMetadata The metadata | ||
* | ||
* @return Boolean | ||
*/ | ||
private function isEntitySupported(ClassMetadata $classMetadata) | ||
{ | ||
return in_array('Knp\DoctrineBehaviors\ORM\SoftDeletable\SoftDeletable', $classMetadata->reflClass->getTraitNames()); | ||
$traitNames = $classMetadata->reflClass->getTraitNames(); | ||
|
||
return in_array('Knp\DoctrineBehaviors\ORM\SoftDeletable\SoftDeletable', $traitNames); | ||
} | ||
|
||
/** | ||
* Returns list of events, that this listener is listening to. | ||
* | ||
* @return array | ||
*/ | ||
public function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::onFlush, | ||
]; | ||
return [Events::onFlush]; | ||
} | ||
} |