Soft Delete

Hi guys,

What is the best way to organise a soft deletion pattern?

When nothing gets removed from the database but the flag is being updated.

The prototype is as follows:

On Delete -> Update table set DeletedFlag=1

on Select -> Select from table where DeletedFlag=0 AND ... Further conditions ....

What I’m thinking now is to extend the ActiveRecordBehavior class and add the following

   protected $SoftDeleteColumn = 'Deleted'; // Column name


     public function beforeDelete( CEvent $oEvent )


         parent::beforeDelete( $oEvent );

         if($this->SoftDeleteColumn!=null and $oEvent->isValid and !$oEvent->handled)


       if ( $oEvent->sender->hasAttribute( $this->SoftDeleteColumn))


     $oEvent->isValid = false;

                                    $oEvent->handled = true;

     $oEvent->sender->setAttribute( $this->SoftDeleteColumn, 1 );




Then to override beforeFind so it extends the original one by adding a criteria of

WHERE SoftDeleteColumn=0

Will beforeFind fire for Find, FindbyPk,findall functions? Are there any instances I should be aware of?

What are the pros and cons of this approach? Kindly advise.

I appreciate your time



I googled forums and was told that

How do I deal with this? Thanks

You will want to use scopes (see the default scope in that page) and to attach behaviors to your ARs.