-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit de51efd
Showing
12 changed files
with
714 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# IntelliJ project files | ||
.idea | ||
|
||
# Composer | ||
composer.lock | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 rahulabs | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
Yii2 SoftDelete | ||
=============== | ||
|
||
|
||
Soft delete extension for Yii2 framework. | ||
|
||
This extension ensures that soft-deleted has delete native consistent behavior and is IDE-friendly. | ||
|
||
Installation | ||
------------ | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require --prefer-dist rahulabs/yii2-softdelete "*" | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"rahulabs/yii2-softdelete": "*" | ||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
Once the extension is installed, simply use it in your code by : | ||
|
||
Edit model class: | ||
```php | ||
use rahulabs\softdelete\behaviors\SoftDeleteBehavior; | ||
use rahulabs\softdelete\SoftDelete; | ||
|
||
class Model extends \yii\db\ActiveRecord | ||
{ | ||
use SoftDelete; | ||
|
||
public function behaviors() | ||
{ | ||
return [ | ||
'class' => SoftDeleteBehavior::className(), | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
Change database table structures, add `deleted_at (int 11)` field and attached to UNIQUE index. | ||
|
||
API | ||
--- | ||
|
||
### ActiveRecord class (SoftDelete Trait): | ||
|
||
The find series method will return `rahulabs\softdelete\ActiveQuery` Object. | ||
|
||
+ softDelete() Deleting data using soft delete mode | ||
+ forceDelete() Use physical deletion mode to force delete data | ||
+ restore() Recover soft deleted model data | ||
+ isTrashed() Whether it is soft deleted | ||
|
||
The following commands are `find()` / `findOne()` / `findAll()` Corresponding versions in different modes : | ||
|
||
All models (including soft deleted ones): | ||
|
||
+ findWithTrashed() | ||
+ findOneWithTrashed($condition) | ||
+ findAllWithTrashed($condition) | ||
|
||
Find only soft deleted models : | ||
|
||
+ findOnlyTrashed() | ||
+ findOneOnlyTrashed($condition) | ||
+ findAllOnlyTrashed($condition) | ||
|
||
The following commands have been rewritten as soft delete versions : | ||
|
||
+ find() | ||
+ findOne() | ||
+ findAll() | ||
+ delete() | ||
|
||
### rahulabs\softdelete\ActiveQuery | ||
|
||
increased `withTrashed()`, `withoutTrashed()` and `onlyTrashed()` Three methods, | ||
Set the corresponding search mode。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "rahulabs/yii2-softdelete", | ||
"description": "Soft delete extension for Yii2 framework", | ||
"type": "yii2-extension", | ||
"keywords": ["yii2","extension","softdelete"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Panlatent", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5.0", | ||
"yiisoft/yii2": "*" | ||
}, | ||
"require-dev": { | ||
"yiisoft/yii2-gii": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"rahulabs\\softdelete\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace rahulabs\softdelete; | ||
|
||
use yii\db\QueryBuilder; | ||
|
||
class ActiveQuery extends \yii\db\ActiveQuery | ||
{ | ||
const WITH_TRASHED = 0; | ||
const WITHOUT_TRASHED = 1; | ||
const ONLY_TRASHED = 2; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $deletedAtAttribute = 'deleted_at'; | ||
/** | ||
* @var int | ||
*/ | ||
private $_trashed; | ||
|
||
/** | ||
* @param QueryBuilder $builder | ||
* @return $this|\yii\db\Query | ||
*/ | ||
public function prepare($builder) | ||
{ | ||
$query = parent::prepare($builder); | ||
switch ($this->getTrashed()) { | ||
case static::WITHOUT_TRASHED: | ||
$query->andWhere(['is', $this->deletedAtAttribute, null]); | ||
break; | ||
case static::ONLY_TRASHED: | ||
$query->andWhere(['!=', $this->deletedAtAttribute, '']); | ||
break; | ||
case static::WITH_TRASHED: // No break; | ||
default: | ||
break; | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
public function withTrashed() | ||
{ | ||
$this->_trashed = static::WITH_TRASHED; | ||
|
||
return $this; | ||
} | ||
|
||
public function withoutTrashed() | ||
{ | ||
$this->_trashed = static::WITHOUT_TRASHED; | ||
|
||
return $this; | ||
} | ||
|
||
public function onlyTrashed() | ||
{ | ||
$this->_trashed = static::ONLY_TRASHED; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTrashed() | ||
{ | ||
if ($this->_trashed === null) { | ||
$this->_trashed = static::WITHOUT_TRASHED; | ||
} | ||
|
||
return $this->_trashed; | ||
} | ||
} |
Oops, something went wrong.