Created
September 17, 2012 07:50
-
-
Save lorenzo/3736076 to your computer and use it in GitHub Desktop.
Soft delete in cakephp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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