dev-master
9999999-dev https://github.com/zunderbolt/cakephp-softdelete-traitSoft delete trait for CakePHP models
MIT
The Requires
- php >=5.4.0
- composer/installers *
plugin cakephp trait model delete softdelete soft
Soft delete trait for CakePHP models
Soft delete trait for using with CakePHP 2.x models., (*1)
Sometimes it is useful to flag records as deleted instead of physically removing them from the database. Achieving that with this plugin requires no custom delete method calls or custom find conditions to exclude deleted records - your models delete() and find()/read() will just work. It is also possible to fetch deleted records when needed or perform "hard" delete., (*2)
"require": { "zunderbolt/cakephp-softdelete-trait": "dev-master" }
APP/Plugin/SoftDeleteTrait
CakePlugin::load('SoftDeleteTrait');
in bootstrap.php
or where needed.App::uses('SoftDeleteTrait', 'SoftDeleteTrait.Lib');
or load it with your autoloader._deletedFieldName
, which should return the name of database field for deletion tracking.Example:, (*3)
App::uses('SoftDeleteTrait', 'SoftDeleteTrait.Lib'); class Item extends AppModel { use SoftDeleteTrait; protected function _deletedFieldName() { return 'isDeleted'; }
Soft delete and exclusion of deleted records from find/read results works automagically. To include deleted records in find results use $this->includeDeletedRecords() in your model. To exclude them again use the excludeDeletedRecords() method., (*4)
To save some additional data for deleted records you can implement protected method _additionalFieldsForDeletedRecord() in your model. For example, you can set record's status and removal reason:, (*5)
protected function _additionalFieldsForDeletedRecord() { return [ 'removalReason' => 'Useless stuff', 'status' => self::STATUS_DELETED ]; }
Soft delete trait for CakePHP models
MIT
plugin cakephp trait model delete softdelete soft