Skip to content

Commit

Permalink
added soft-deletable comments
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Mar 23, 2012
1 parent 53990a5 commit abeb08e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
26 changes: 25 additions & 1 deletion src/Knp/DoctrineBehaviors/ORM/SoftDeletable/SoftDeletable.php
@@ -1,7 +1,21 @@
<?php

/*
* 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.
*/

namespace Knp\DoctrineBehaviors\ORM\SoftDeletable;

/**
* SoftDeletable trait.
*
* Should be used inside entity, that needs to be self-deleted.
*/
trait SoftDeletable
{
/**
Expand All @@ -10,18 +24,28 @@ trait SoftDeletable
private $deletedAt;

/**
* @ORM\PreRemove
* Marks entity as deleted.
*/
public function delete()
{
$this->deletedAt = new \DateTime();
}

/**
* Checks whether the entity was been deleted.
*
* @return Boolean
*/
public function isDeleted()
{
return null !== $this->deletedAt;
}

/**
* Returns date on which entity was been deleted.
*
* @return DateTime|null
*/
public function getDeletedAt()
{
return $this->deletedAt;
Expand Down
@@ -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];
}
}

0 comments on commit abeb08e

Please sign in to comment.