https://github.com/cornernote/yii2-softdelete
Soft delete behavior for Yii2
https://github.com/cornernote/yii2-softdelete
Last synced: about 1 month ago
JSON representation
Soft delete behavior for Yii2
- Host: GitHub
- URL: https://github.com/cornernote/yii2-softdelete
- Owner: cornernote
- License: other
- Created: 2015-06-20T07:44:24.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-09-13T23:13:07.000Z (over 7 years ago)
- Last Synced: 2025-02-25T15:13:55.512Z (about 2 months ago)
- Language: PHP
- Size: 46.9 KB
- Stars: 23
- Watchers: 2
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-yii2 - cornernote/yii2-softdelete
README
# Yii2 Soft Delete
[](https://github.com/cornernote/yii2-softdelete/tags)
[](LICENSE.md)
[](https://travis-ci.org/cornernote/yii2-softdelete)
[](https://scrutinizer-ci.com/g/cornernote/yii2-softdelete/code-structure)
[](https://scrutinizer-ci.com/g/cornernote/yii2-softdelete)
[](https://packagist.org/packages/cornernote/yii2-softdelete)Soft delete behavior for Yii2.
## Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
$ composer require cornernote/yii2-softdelete "*"
```or add
```
"cornernote/yii2-softdelete": "*"
```to the `require` section of your `composer.json` file.
## Usage
In your ActiveRecord class:
```php
public function behaviors() {
return [
\cornernote\softdelete\SoftDeleteBehavior::className(),
// or
[
'class' => \cornernote\softdelete\SoftDeleteBehavior::className(),
'attribute' => 'deleted_time',
'value' => new \yii\db\Expression('NOW()'), // for sqlite use - new \yii\db\Expression("date('now')")
],
];
}
```then you can use explicitly `$model->softDelete()`, `$model->hardDelete()`
and `$model->unDelete()` (for softly deleted models). Each of these methods return
boolean result. Also `$model->softDelete()` calls indirectly from `$model->delete()`,
which always returns `false`.In your ActiveQuery class:
```php
public function behaviors() {
return [
\cornernote\softdelete\SoftDeleteQueryBehavior::className(),
// or
[
'class' => \cornernote\softdelete\SoftDeleteQueryBehavior::className(),
'attribute' => 'deleted_time',
],
];
}
```