Cleanest Way To Perform A Soft Delete?

I’m working on an application where records should never actually be deleted from the database. Instead, I ‘soft-delete’ them by updating a status column.

My question is how best to implement this. I would like all delete*() methods to continue working the same way, and I’d like to modify as little code as possible. Examining the internals of deleteByPk() I see that the key line is this one:




public function deleteByPk($pk,$condition='',$params=array())

{

    Yii::trace(get_class($this).'.deleteByPk()','system.db.ar.CActiveRecord');

    $builder=$this->getCommandBuilder();

    $criteria=$builder->createPkCriteria($this->getTableSchema(),$pk,$condition,$params);


    // THIS LINE:::

    $command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);

    return $command->execute();

}



Using a behavior (with beforeDelete() hook) doesn’t fit my specs because (1) it only affects delete() commands and (2) it would mean delete() always returns false, even when the record has been soft-deleted.

Creating a child AR class that modifies each delete*() method is tedious and increases coupling, but seems simple and doable.

Creating a custom DbCommandBuilder class that handles deletes as soft-deletes seems like the “right” way, but also complicated. I’m not sure how that could even be attached to specific AR models. But the usage might be decoupled and cool:




// (Pseudo-code...)

$model = new Model();

$model->setBuilder('SoftDeleteDbCommandBuilder');

$model->delete(); // does a soft delete


$model->setBuilder(null);

$model->delete(); // does a normal delete



Any thoughts?

See this:

http://www.packtpub.com/sites/default/files/5481OS-Chapter-8-Extending-Yii.pdf

There is a softDeleteBehavior that should work for you that doesn’t use a beforeDelete event.

Thanks! (Great link.)

I ended up writing a behavior anyway though. It adds ‘soft’ versions of Yii’s built-in delete methods: Github: Soft-Delete AR Behavior