Soft delete for CakePHP 2.x models. Transparently flags records as deleted instead of actually removing them. Will also then hide the deleted records from find operations.
If you're using composer then just add the following to your require block.
"burriko/cake-soft-delete": "2.0.*@dev"
If you're not, then clone/copy the contents of this directory to app/Plugins/CakeSoftDelete.
-
Add the following line to your app/Config/bootstrap.php.
CakePlugin::load('CakeSoftDelete');
-
Change your AppModel to extend SoftDeletableModel. You'll also need to add an appropriate App::uses to tell Cake where to load SoftDeletableModel. Basically Your AppModel class should start something like this.
<?php App::uses('SoftDeletableModel', 'CakeSoftDelete.Model'); class AppModel extends SoftDeletableModel
-
In the model that should be soft deletable add:
public $actsAs = array('CakeSoftDelete.SoftDeletable');
-
Your model's database schema will need a field to act as a flag for whether a record has been deleted. By default this field is called 'deleted', but this can be changed in the behavior's settings. The field can be either a boolean or datetime (in which case it will be set to the time the field was deleted).
Now when you delete records from this model they should just be soft deleted instead. Soft deleted records will not be present in results from find functions.
To include deleted records in results call the includeDeletedRecords() method on the model. You can then call excludeDeletedRecords() to hide them again.
Deletes will be cascaded to related models as usual, except for HABTM relations. This hasn't been a problem for me but is something to be aware of.