You are browsing a version that is no longer maintained.

Soft Delete Extension

Sometimes you may not want to delete data from your database completely, but you want to disable or temporarily delete some records so they do not appear anymore in your frontend. Then, later you might want to restore that deleted data like it was never deleted.

This is possible with the SoftDelete extension which can be found on github.

Installation

First you just need to get the code by cloning the github repository:

$ git clone git://github.com/doctrine/mongodb-odm-softdelete.git

Now once you have the code you can setup the autoloader for it:

1<?php $classLoader = new ClassLoader('Doctrine\ODM\MongoDB\SoftDelete', 'mongodb-odm-softdelete/lib'); $classLoader->register();
2
3
4

Setup

Now you can autoload the classes you need to setup the SoftDeleteManager instance you need to manage the soft delete state of your documents:

1<?php use Doctrine\ODM\MongoDB\SoftDelete\Configuration; use Doctrine\ODM\MongoDB\SoftDelete\UnitOfWork; use Doctrine\ODM\MongoDB\SoftDelete\SoftDeleteManager; use Doctrine\Common\EventManager; // $dm is a DocumentManager instance we should already have $config = new Configuration(); $evm = new EventManager(); $sdm = new SoftDeleteManager($dm, $config, $evm);
2
3
4
5
6
7
8
9
10
11
12

SoftDeleteable Interface

In order for your documents to work with the SoftDelete functionality they must implement the SoftDeleteable interface:

1<?php interface SoftDeleteable { function getDeletedAt(); }
2
3
4
5
6

Example Implementation

An implementation might look like this in a User document:

1<?php use Doctrine\ODM\MongoDB\SoftDelete\SoftDeleteable; /** @mongodb:Document */ class User implements SoftDeleteable { // ... /** @mongodb:Date @mongodb:Index */ private $deletedAt; public function getDeletedAt() { return $this->deletedAt; } // ... }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Usage

Once you have the $sdm you can start managing the soft delete state of your documents:

1<?php $jwage = $dm->getRepository('User')->findOneByUsername('jwage'); $fabpot = $dm->getRepository('User')->findOneByUsername('fabpot'); $sdm->delete($jwage); $sdm->delete($fabpot); $sdm->flush();
2
3
4
5
6
7

The call to SoftDeleteManager#flush() would persist the deleted state to the database for all the documents it knows about and run a query like the following:

1db.users.update({ _id : { $in : userIds }}, { $set : { deletedAt : new Date() } })

Now if we were to restore the documents:

1<?php $sdm->restore($jwage); $sdm->flush();
2
3
4

It would execute a query like the following:

1db.users.update({ _id : { $in : userIds }}, { $unset : { deletedAt : true } })

Events

We trigger some additional lifecycle events when documents are soft deleted and restored:

  • Events::preSoftDelete
  • Events::postSoftDelete
  • Events::preRestore
  • Events::postRestore

Using the events is easy, just define a class like the following:

1<?php class TestEventSubscriber implements \Doctrine\Common\EventSubscriber { public function preSoftDelete(LifecycleEventArgs $args) { $document = $args->getDocument(); $sdm = $args->getSoftDeleteManager(); } public function getSubscribedEvents() { return array(Events::preSoftDelete); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Now we just need to add the event subscriber to the EventManager:

1<?php $eventSubscriber = new TestEventSubscriber(); $evm->addEventSubscriber($eventSubscriber);
2
3
4

When we soft delete something the preSoftDelete() method will be invoked before any queries are sent to the database:

1<?php $sdm->delete($fabpot); $sdm->flush();
2
3
4

Cascading Soft Deletes

You can easily implement cascading soft deletes by using events in a certain way. Imagine you have a User and Post document and you want to soft delete a users posts when you delete him.

You just need to setup an event listener like the following:

1<?php use Doctrine\Common\EventSubscriber; use Doctrine\ODM\MongoDB\SoftDelete\Event\LifecycleEventArgs; class CascadingSoftDeleteListener implements EventSubscriber { public function preSoftDelete(LifecycleEventArgs $args) { $sdm = $args->getSoftDeleteManager(); $document = $args->getDocument(); if ($document instanceof User) { $sdm->deleteBy('Post', array('user.id' => $document->getId())); } } public function preRestore(LifecycleEventArgs $args) { $sdm = $args->getSoftDeleteManager(); $document = $args->getDocument(); if ($document instanceof User) { $sdm->restoreBy('Post', array('user.id' => $document->getId())); } } public function getSubscribedEvents() { return array( Events::preSoftDelete, Events::preRestore ); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

Now when you delete an instance of User it will also delete any Post documents where they reference the User being deleted. If you restore the User, his Post documents will also be restored.