Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulabs committed Aug 6, 2024
0 parents commit de51efd
Show file tree
Hide file tree
Showing 12 changed files with 714 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# IntelliJ project files
.idea

# Composer
composer.lock
/vendor/
21 changes: 21 additions & 0 deletions LICENSE
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.
90 changes: 90 additions & 0 deletions README.md
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。
25 changes: 25 additions & 0 deletions composer.json
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/"
}
}
}
73 changes: 73 additions & 0 deletions src/ActiveQuery.php
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;
}
}
Loading

0 comments on commit de51efd

Please sign in to comment.