Last active
December 26, 2015 12:39
-
-
Save thomseddon/7153307 to your computer and use it in GitHub Desktop.
CakePHP Soft Delete Behaviour
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
<?php | |
App::uses('ModelBehavior', 'Model'); | |
class SoftDeleteBehavior extends ModelBehavior { | |
public function setup(Model $Model, $settings = array()) { | |
$field = isset($settings['field']) ? $settings['field'] : 'is_deleted'; | |
$this->settings[$Model->alias]['field'] = $field; | |
} | |
public function beforeFind(Model $Model, $query) { | |
$field = $Model->escapeField($this->settings[$Model->alias]['field']); | |
if (!isset($query['conditions'][$field])) { | |
$query['conditions'][$field] = false; | |
} | |
return $query; | |
} | |
public function softDelete(Model $Model) { | |
return $Model->saveField($this->settings[$Model->alias]['field'], true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment