Soft Deletable Behavior
This behavior will let you implement soft deletion in your CakePHP models so no real data is lost when you issue a delete on a specific record. Instead, a field of your choosing is used to indicate that a record has been soft deleted, and the behavior will automatically override your specific find operations so only non-soft deleted records are fetched.
Quick Introduction
Say we have an Article model which has a field named ‘deleted’ and another one named ‘deleted_date’, among others. We want to set the deleted field to 1, and the deleted_date to have the actual date whenever a record is deleted, instead of permanently delete the record. Furthermore, we want the ability to later on undelete a record, or permanently purge it, among other functionalities. All we have to do then is add the Soft Deletable behavior to the list of behaviors supported for this model, and then use standard CakePHP methods to do our deletion, and the built in behavior methods to undelete the records:
class Article extends AppModel {
var $name = 'Article';
var $actsAs = array('SoftDeletable');
}
We now easily do:
// Soft delete record 1 & 2 $this->Article->delete(1); $this->Article->delete(2); // Let's undelete record 2 $this->Article->undelete(2); // And now let's purge so soft deleted records are permanently deleted $this->Article->purge()
It’s that easy! Go ahead and read the tutorial at the bakery to learn all there is to learn.
Download
To download Sluggable Behavior and other Cake Syrup ingredients go to the download page.
