fuel/orm — Issue #105: Soft Delete/Archive Row
Author: JesseObrien
Created: 2011-10-17 19:00:41+00:00
State: open

I'm thinking it might be beneficial to include a soft-delete feature that uses a ```deleted_at``` field in the database to allow a row to be referenced as "deleted", but not removed from the database. I work in the medical field where we aren't ever allowed to actually delete data from a database, so this would be handy. I have some code roughly laid out, but I'd need some help figuring out how to get queries to use it as a filter.

I'm thinking the syntax would be something like below.
*Disclaimer: This is probably poorly designed, it's just an idea.*

```php

frozen()){
return false;
}

$this->observe('before_soft_delete');
$this->{static::$property} = $mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp();
$this->save();
$this->observe('after_soft_delete');
return $this;
}

/**
* Restores a soft deleted object
* @param string
*/
public function restore( $property = 'deleted_at' ){
if($this->frozen()) {
return false;
}

// This might need some refactoring
if( $this->is_soft_deleted() ){
$this->observe('before_restore');
$this->{static::$property} = 0;
$this->save();
$this->observe('after_restore');
}
return $this;
}

***app\classes\model\test.php***

// Syntax for observers and turning on soft delete
class Model_Test extends Orm\Model{
protected static $_observers = array(
'Orm\\Observer_DeletedAt' => array('before_soft_delete'),
'Orm\\Observer_Restore' => array('before_restore'),
)
protected static $_soft_delete = true;
}

```