sibds / yii2-trash-behavior
AR models behavior that allows to mark models as deleted and then restore them.
Installs: 2 489
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 6
Forks: 0
Open Issues: 0
Type:yii2-behaviors
Requires
- php: >=5.4.0
- yiisoft/yii2: ~2.0.5
Requires (Dev)
- yiisoft/yii2-codeception: ~2.0
- yiisoft/yii2-debug: 2.0.*@dev
This package is auto-updated.
Last update: 2024-12-03 08:43:20 UTC
README
AR models behavior that allows to mark models as deleted and then restore them.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require sibds/yii2-trash-behavior
or add
"sibds/yii2-trash-behavior": "*"
to the require
section of your composer.json
file.
How to use?
Paste in the model code:
public function behaviors() { return [ \sibds\behaviors\TrashBehavior::className(), ]; }
To mark a record for deletion, call function delete
:
$model->delete();
The second call delete
, delete the record.
To restore record:
$model->restore();
For the correct selection of records in the model, it is necessary to redefine the function of find
:
public static function find() { return (new \sibds\behaviors\TrashQuery(get_called_class()))->findRemoved(); }
Example of use:
$posts = Post::find()->all();
Selecting only marked for deletion of records:
$count = Post::find()->onlyRemoved()->count();
Selecting all records:
$allPosts = Post::find()->withRemoved()->all();