sibds/yii2-trash-behavior

AR 模型行为,允许标记模型为已删除,然后恢复它们。

v0.4.2 2017-05-17 19:49 UTC

This package is auto-updated.

Last update: 2024-09-03 07:58:58 UTC


README

AR 模型行为,允许标记模型为已删除,然后恢复它们。

Build Status Scrutinizer Code Quality Code Coverage

安装

安装此扩展的首选方式是通过 composer

可以运行

$ composer require sibds/yii2-trash-behavior

或者将以下内容添加到您的 composer.json 文件的 require 部分。

"sibds/yii2-trash-behavior": "*"

require

如何使用?

将以下代码粘贴到模型中

  public function behaviors()
  {
      return [
          \sibds\behaviors\TrashBehavior::className(),
      ];
  }

要标记记录为删除,请调用函数 delete

  $model->delete();

第二次调用 delete,将删除记录。

要恢复记录

  $model->restore();

为了在模型中正确选择记录,需要重新定义函数 find

  public static function find()
  {
      return (new \sibds\behaviors\TrashQuery(get_called_class()))->findRemoved();
  }

使用示例

  $posts = Post::find()->all();

选择仅标记为删除的记录

  $count = Post::find()->onlyRemoved()->count();

选择所有记录

  $allPosts = Post::find()->withRemoved()->all();