Skip to content

Instantly share code, notes, and snippets.

@lorenzo
Created September 17, 2012 07:50
Show Gist options
  • Save lorenzo/3736076 to your computer and use it in GitHub Desktop.
Save lorenzo/3736076 to your computer and use it in GitHub Desktop.
Soft delete in cakephp
/**
* Override Model::delete() to implement a soft-delete feature for this model
* requires the model to have a 'deleted' boolean column
*
* @return boolean
**/
public function delete($id = null, $cascade = true) {
if ($id === null) {
$id = $this->id;
}
$this->id = $id;
$event = new CakeEvent('Model.beforeDelete', $this, array($cascade));
list($event->break, $event->breakOn) = array(true, array(false, null));
$this->getEventManager()->dispatch($event);
if (!$event->isStopped()) {
if (!$this->exists()) {
return false;
}
$this->_deleteDependent($id, $cascade);
$this->_deleteLinks($id);
$this->id = $id;
$data = array(
$this->primaryKey => $id,
'deleted' => true
);
if ($this->save($data, array('validate' => false, 'callbacks' => false))) {
$this->getEventManager()->dispatch(new CakeEvent('Model.afterDelete', $this));
$this->_clearCache();
$this->id = false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment