2017 © Pedro Peláez
 

cakephp-plugin cakephp-softdelete-trait

Soft delete trait for CakePHP models

image

zunderbolt/cakephp-softdelete-trait

Soft delete trait for CakePHP models

  • Wednesday, December 3, 2014
  • by ZunderBolt
  • Repository
  • 1 Watchers
  • 5 Stars
  • 34 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

CakePHP Soft Delete Trait

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)

Requirements

  • CakePHP 2.x
  • PHP 5.4+

Installation

With Composer

"require": {
    "zunderbolt/cakephp-softdelete-trait": "dev-master"
}

Manual

  • Download files to APP/Plugin/SoftDeleteTrait

Configuration

  • Load plugin with CakePlugin::load('SoftDeleteTrait'); in bootstrap.php or where needed.
  • Show the app where to look for source file App::uses('SoftDeleteTrait', 'SoftDeleteTrait.Lib'); or load it with your autoloader.
  • Use the trait in your model and implement method _deletedFieldName, which should return the name of database field for deletion tracking.
  • Don't forget to add the field in your database table. It can be boolean flag (TINYINT/INT field, 1 for deleted record, 0 otherwise), date or datetime (NULL for normal record, date/datetime of removal for deleted record).

Example:, (*3)

    App::uses('SoftDeleteTrait', 'SoftDeleteTrait.Lib');

    class Item extends AppModel {
        use SoftDeleteTrait;

        protected function _deletedFieldName() {
            return 'isDeleted';
        }

Usage notes

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
        ];
    }

The Versions

03/12 2014

dev-master

9999999-dev https://github.com/zunderbolt/cakephp-softdelete-trait

Soft delete trait for CakePHP models

  Sources   Download

MIT

The Requires

 

plugin cakephp trait model delete softdelete soft