zunderbolt/cakephp-softdelete-trait 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zunderbolt/cakephp-softdelete-trait

Composer 安装命令:

composer require zunderbolt/cakephp-softdelete-trait

包简介

Soft delete trait for CakePHP models

README 文档

README

Soft delete trait for using with CakePHP 2.x models.

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.

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:

    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.

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:

    protected function _additionalFieldsForDeletedRecord() {
        return [
            'removalReason' => 'Useless stuff',
            'status'        => self::STATUS_DELETED
        ];
    }

统计信息

  • 总下载量: 1.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-03