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/).
Either run
```
php composer.phar require --prefer-dist vyants/yii2-softdelete "*"
php composer.phar require --prefer-dist cornernote/yii2-softdelete "*"
```
or add
```
"vyants/yii2-softdelete": "*"
"cornernote/yii2-softdelete": "*"
```
to the require section of your `composer.json` file.
Usage
-----
## Usage
In your ActiveRecord class:
```
public function behaviors() {
```php
public function behaviors() {
return [
'softDelete' => ['class' => 'vyants\softdelete\SoftDelete',
'statusAttribute' => 'status',
'timeAttribute' => false,
'deletedValue' => -1,
'activeValue' => 1,
[
'class' => 'cornernote\behaviors\SoftDeleteBehavior',
'attribute' => 'deleted_time',
'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;
* ```
* public function behaviors() {
* return [
* 'softDelete' => [
* [
* 'class' => 'cornernote\behaviors\SoftDeleteBehavior',
* 'attribute' => 'delete_time',
* 'value' => new Expression('NOW()'),
......
......@@ -12,7 +12,7 @@ use yii\db\ActiveQuery;
* ```
* public function behaviors() {
* return [
* 'softDelete' => [
* [
* 'class' => 'cornernote\behaviors\SoftDeleteQueryBehavior',
* 'attribute' => 'delete_time',
* ],
......@@ -31,14 +31,20 @@ class SoftDeleteQueryBehavior extends Behavior
*/
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