Commit a7fb187c authored by cornernote's avatar cornernote

update ActiveQuery behavior and update examples

parent 2d48edf1
Soft delete behavior for Yii2 # Yii2 Soft Delete
=======================
## Installation
Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run Either run
``` ```
php composer.phar require --prefer-dist vyants/yii2-softdelete "*" php composer.phar require --prefer-dist cornernote/yii2-softdelete "*"
``` ```
or add or add
``` ```
"vyants/yii2-softdelete": "*" "cornernote/yii2-softdelete": "*"
``` ```
to the require section of your `composer.json` file. to the require section of your `composer.json` file.
Usage ## Usage
-----
In your ActiveRecord class:
```
public function behaviors() { ```php
return [ public function behaviors() {
'softDelete' => ['class' => 'vyants\softdelete\SoftDelete', return [
'statusAttribute' => 'status', [
'timeAttribute' => false, 'class' => 'cornernote\behaviors\SoftDeleteBehavior',
'deletedValue' => -1, 'attribute' => 'deleted_time',
'activeValue' => 1, 'value' => new Expression('NOW()'),
], ],
]; ];
} }
``` ```
\ No newline at end of file
In your ActiveQuery class:
```php
public function behaviors() {
return [
[
'class' => 'cornernote\behaviors\SoftDeleteQueryBehavior',
'attribute' => 'deleted_time',
],
];
}
```
\ No newline at end of file
...@@ -14,7 +14,7 @@ use yii\db\Expression; ...@@ -14,7 +14,7 @@ use yii\db\Expression;
* ``` * ```
* public function behaviors() { * public function behaviors() {
* return [ * return [
* 'softDelete' => [ * [
* 'class' => 'cornernote\behaviors\SoftDeleteBehavior', * 'class' => 'cornernote\behaviors\SoftDeleteBehavior',
* 'attribute' => 'delete_time', * 'attribute' => 'delete_time',
* 'value' => new Expression('NOW()'), * 'value' => new Expression('NOW()'),
......
...@@ -12,7 +12,7 @@ use yii\db\ActiveQuery; ...@@ -12,7 +12,7 @@ use yii\db\ActiveQuery;
* ``` * ```
* public function behaviors() { * public function behaviors() {
* return [ * return [
* 'softDelete' => [ * [
* 'class' => 'cornernote\behaviors\SoftDeleteQueryBehavior', * 'class' => 'cornernote\behaviors\SoftDeleteQueryBehavior',
* 'attribute' => 'delete_time', * 'attribute' => 'delete_time',
* ], * ],
...@@ -31,14 +31,20 @@ class SoftDeleteQueryBehavior extends Behavior ...@@ -31,14 +31,20 @@ class SoftDeleteQueryBehavior extends Behavior
*/ */
public $attribute = 'deleted_at'; public $attribute = 'deleted_at';
public function deleteds() /**
* @return static
*/
public function deleted()
{ {
return $this->owner->andWhere($this->attribute . ' IS NULL'); return $this->owner->andWhere($this->attribute . ' IS NOT NULL');
} }
public function undeleteds() /**
* @return static
*/
public function notDeleted()
{ {
return $this->owner->andWhere($this->attribute . ' IS NOT NULL'); return $this->owner->andWhere($this->attribute . ' IS NULL');
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment