Implementing softdelete with cakephp

Softdeleting in terms of database generally means marking the record as delete rather than physically removing the record from the db. It is specifically useful in cases like CRMs where we want to retain the data for future reference in the database. There are a few behaviors to implement Softdelete behavior in cakephp. We researched on them and finally decided to use cakesyrup’s softdeleteable behavior. Another good option is to use CakeDC Utils SoftDelete Behavior but at the time of this writing the behavior required quite a few changes for it to be used effectively. How to Use Softdeleteable in a cakephp application
  • Download the behavior from the sourceforge on the above said link and copy it to app/Model/Behavior
  • Add the behavior to the $actsAs member of the model that needs to be softdeleted:
    class User extends AppModel {
    var $actsAs = array('SoftDeletable');
  • By Default it works on two fields in a table named deleted, delete_date, but if you want to use the different fields from your table, you can configure that in the options to the Softdeletable behavior:
    var $actsAs = array('SoftDelete'=> array('field' => '0'));
  • Thats it. Now whenever you perform a delete action on certain model, then it won’t delete that record permanently instead will set deleted as 1 hence it won’t be resulting in the finder queries that you shoot for the model. Enjoy retaining your records from deleting permanently !!