You are currently reading the 1.2 documentation. Switch to 2.0 

SoftDelete

The SoftDelete behavior is a very simple yet highly desired model behavior which overrides the delete() functionality and adds a deleted_at column. When delete() is called, instead of deleting the record from the database, a delete_at date is set. Below is an example of how to create a model with the SoftDelete behavior being used.

// models/SoftDeleteTest.php

class SoftDeleteTest extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('name', 'string', null, array(
                'primary' => true
            )
        );
    }

    public function setUp()
    {
        $this->actAs('SoftDelete');
    }
}

Here is the same example in YAML format. You can read more about YAML in the YAML Schema Files chapter:

---
# schema.yml

# ...
SoftDeleteTest:
  actAs: [SoftDelete]
  columns:
    name:
      type: string(255)
      primary: true

Lets check the SQL that is generated by the above models:

// test.php

// ...
$sql = Doctrine_Core::generateSqlFromArray(array('SoftDeleteTest'));
echo $sql[0];

The above code would output the following SQL query:

CREATE TABLE soft_delete_test (name VARCHAR(255), 
deleted_at DATETIME DEFAULT NULL,
PRIMARY KEY(name)) ENGINE = INNODB

Now lets put the behavior in action.

You are required to enable DQL callbacks in order for all executed queries to have the dql callbacks executed on them. In the SoftDelete behavior they are used to filter the select statements to exclude all records where the deleted_at flag is set with an additional WHERE condition.

Enable DQL Callbacks

// bootstrap.php

// ...
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);

Now save a new record so we can test the SoftDelete functionality:

// test.php

// ...
$record = new SoftDeleteTest();
$record->name = 'new record';
$record->save();

Now when we call delete() the deleted_at flag will be set to true:

// test.php

// ...
$record->delete();

print_r($record->toArray());

The above example would produce the following output:

$ php test.php
Array
(
    [name] => new record
    [deleted_at] => 2009-09-01 00:59:01
)

Also, when we select some data the query is modified for you:

// test.php

// ...
$q = Doctrine_Query::create()
    ->from('SoftDeleteTest t');

echo $q->getSqlQuery();

The above call to getSql() would output the following SQL query:

SELECT 
s.name AS s__name,
s.deleted_at AS s__deleted_at
FROM soft_delete_test s
WHERE (s.deleted_at IS NULL)

Notice how the where condition is automatically added to only return the records that have not been deleted.

Now if we execute the query:

// test.php

// ...
$count = $q->count();
echo $count;

The above would be echo 0 because it would exclude the record saved above because the delete flag was set.


Questions and Feedback

If you find a problem with the documentation or have a suggestion, please register and open a ticket.

If you need support or have a technical question, you can post to the user mailing-list.