SoftDeleteable behavior allows to "soft delete" objects, filtering them at SELECT time by marking them as with a timestamp, but not explicitly removing them from the database.
Features:
Content:
Read the documentation or check the example code on how to setup and use the extensions in most optimized way.
With SoftDeleteable there's one more step you need to do. You need to add the filter to your configuration:
$config = new Doctrine\ORM\Configuration;
// Your configs..
$config->addFilter('soft-deleteable', 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
And then you can access the filter from your EntityManager to enable or disable it with the following code:
// This will enable the SoftDeleteable filter, so entities which were "soft-deleted" will not appear
// in results
// You should adapt the filter name to your configuration (ex: softdeleteable)
$em->getFilters()->enable('soft-deleteable');
// This will disable the SoftDeleteable filter, so entities which were "soft-deleted" will appear in results
$em->getFilters()->disable('soft-deleteable');
Available configuration options:
Note: that SoftDeleteable interface is not necessary, except in cases there you need to identify entity as being SoftDeleteable. The metadata is loaded only once then cache is activated
<?php
namespace Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class Article
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\Column(name="title", type="string")
*/
private $title;
/**
* @ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function getDeletedAt()
{
return $this->deletedAt;
}
public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
}
}
Yaml mapped Article: /mapping/yaml/Entity.Article.dcm.yml
---
Entity\Article:
type: entity
table: articles
gedmo:
soft_deleteable:
field_name: deletedAt
time_aware: false
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string
deletedAt:
type: date
nullable: true
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
<entity name="Mapping\Fixture\Xml\Timestampable" table="timestampables">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="title" type="string" />
<field name="deletedAt" type="datetime" nullable="true" />
<gedmo:soft-deleteable field-name="deletedAt" time-aware="false" />
</entity>
</doctrine-mapping>
<?php
$article = new Article;
$article->setTitle('My Article');
$em->persist($article);
$em->flush();
// Now if we remove it, it will set the deletedAt field to the actual date
$em->remove($article);
$em->flush();
$repo = $em->getRepository('Article');
$art = $repo->findOneBy(array('title' => 'My Article'));
// It should NOT return the article now
$this->assertNull($art);
// But if we disable the filter, the article should appear now
$em->getFilters()->disable('soft-deleteable');
$art = $repo->findOneBy(array('title' => 'My Article'));
$this->assertTrue(is_object($art));
// Enable / Disable filter filter, for specified entity (default is enabled for all)
$filter = $em->getFilters()->enable('soft-deleteable');
$filter->disableForEntity('Entity\Article');
$filter->enableForEntity('Entity\Article');
Easy like that, any suggestions on improvements are very welcome.