Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Feb 1, 2011
0 parents commit 932c8e5
Show file tree
Hide file tree
Showing 20 changed files with 1,925 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitmodules
@@ -0,0 +1,9 @@
[submodule "lib/vendor/doctrine-mongodb-odm"]
path = lib/vendor/doctrine-mongodb-odm
url = git://github.com/doctrine/mongodb-odm.git
[submodule "lib/vendor/doctrine-common"]
path = lib/vendor/doctrine-common
url = git://github.com/doctrine/common.git
[submodule "lib/vendor/doctrine-mongodb"]
path = lib/vendor/doctrine-mongodb
url = git://github.com/doctrine/mongodb.git
114 changes: 114 additions & 0 deletions README.markdown
@@ -0,0 +1,114 @@
# Doctrine MongoDB ODM SoftDelete Functionality

This library gives you some additional classes and API for managing the soft deleted state of Doctrine
MongoDB ODM documents. To get started you just need to configure a few objects and get a SoftDeleteManager
instance:

## Setup

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();
$uow = new UnitOfWork($dm, $config);
$evm = new EventManager();
$sdm = new SoftDeleteManager($dm, $config, $uow, $evm);

## SoftDelete Documents

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

interface SoftDeleteable
{
function getDeletedAt();
function isDeleted();
}

An implementation might look like this:

use Doctrine\ODM\MongoDB\SoftDelete\SoftDeleteable;

/** @mongodb:Document */
class User implements SoftDeleteable
{
// ...

/** @mongodb:Date */
private $deletedAt;

public function getDeletedAt()
{
return $this->deletedAt;
}

public function isDeleted()
{
return $this->deletedAt !== null ? true : false;
}

// ...
}

## Managing Soft Delete State

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

$jwage = $dm->getRepository('User')->findOneByUsername('jwage');
$fabpot = $dm->getRepository('User')->findOneByUsername('fabpot');
$sdm->delete($jwage);
$sdm->delete($fabpot);
$sdm->flush();

The above would issue a simple query setting the deleted date:

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

Now if we were to restore the documents:

$sdm->restore($jwage);
$sdm->flush();

It would unset the deletedAt date:

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

## Events

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

* Events::preSoftDelete
* Events::postSoftDelete
* Events::preSoftDeleteRestore
* Events::postSoftDeleteRestore

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

class TestEventSubscriber implements \Doctrine\Common\EventSubscriber
{
public function preSoftDelete(LifecycleEventArgs $args)
{
$document = $args->getDocument();
$sdm = $args->getSoftDeleteManager();
}

public function getSubscribedEvents()
{
return array(Events::preSoftDelete);
}
}

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

$eventSubscriber = new TestEventSubscriber();
$evm->addEventSubscriber($eventSubscriber);

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

$sdm->delete($fabpot);
$sdm->flush();
60 changes: 60 additions & 0 deletions lib/Doctrine/ODM/MongoDB/SoftDelete/Configuration.php
@@ -0,0 +1,60 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\MongoDB\SoftDelete;

/**
* Configuration class for the SoftDelete functionality.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 1.0
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class Configuration
{
/**
* Array of configurable attributes.
*
* @var array
*/
private $attributes = array(
'deletedFieldName' => 'deletedAt'
);

/**
* Sets the name of the field to use to store the deleted at date.
*
* @param string $deletedFieldName
*/
public function setDeletedFieldName($deletedFieldName)
{
$this->attributes['deletedFieldName'] = $deletedFieldName;
}

/**
* Gets the name of the field used to store the deleted at date.
*
* @return string $deletedFieldName
*/
public function getDeletedFieldName()
{
return isset($this->attributes['deletedFieldName']) ? $this->attributes['deletedFieldName'] : 'deleted';
}
}
72 changes: 72 additions & 0 deletions lib/Doctrine/ODM/MongoDB/SoftDelete/Event/LifecycleEventArgs.php
@@ -0,0 +1,72 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\MongoDB\SoftDelete\Event;

use Doctrine\ODM\MongoDB\SoftDelete\SoftDeleteable;
use Doctrine\ODM\MongoDB\SoftDelete\SoftDeleteManager;
use Doctrine\Common\EventArgs;

/**
* Lifecycle Events are triggered by the UnitOfWork during lifecycle transitions
* of documents.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 1.0
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class LifecycleEventArgs extends EventArgs
{
/**
* @var SoftDeleteManager $sdm
*/
private $sdm;

/**
* @var SoftDeleteable $document
*/
private $document;

public function __construct(SoftDeleteable $document, SoftDeleteManager $sdm)
{
$this->document = $document;
$this->sdm = $sdm;
}

/**
* Gets the SoftDeletable document instance.
*
* @return SoftDeleteable $document
*/
public function getDocument()
{
return $this->document;
}

/**
* Gets the SoftDeleteManager
*
* @return SoftDeleteManager
*/
public function getSoftDeleteManager()
{
return $this->sdm;
}
}
59 changes: 59 additions & 0 deletions lib/Doctrine/ODM/MongoDB/SoftDelete/Events.php
@@ -0,0 +1,59 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\MongoDB\SoftDelete;

/**
* Container for all SoftDelete events.
*
* This class cannot be instantiated.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 1.0
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
final class Events
{
private function __construct() {}

/**
* Invoked before a document is soft deleted in the database.
*/
const preSoftDelete = 'preSoftDelete';

/**
* Invokes after a document is soft deleted in the database.
*/
const postSoftDelete = 'postSoftDelete';


/**
* Invoked before a documents soft delete is removed and document restored in the database.
*/
const preSoftDeleteRestore = 'preSoftDeleteRestore';

/**
* Invoked after a documents soft delete is removed and document restored in the database.
*/
const postSoftDeleteRestore = 'postSoftDeleteRestore';

}

0 comments on commit 932c8e5

Please sign in to comment.