PHPackages                             wodrow/yii2wsoftdelete - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. wodrow/yii2wsoftdelete

ActiveYii2-extension[Database &amp; ORM](/categories/database)

wodrow/yii2wsoftdelete
======================

soft delete for yii2 models

1.0.1(3y ago)0252MITPHPPHP &gt;=7.1

Since Nov 12Pushed 3y ago1 watchersCompare

[ Source](https://github.com/wodrow/yii2wsoftdelete)[ Packagist](https://packagist.org/packages/wodrow/yii2wsoftdelete)[ RSS](/packages/wodrow-yii2wsoftdelete/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (2)

Yii2 SoftDelete
===============

[](#yii2-softdelete)

[![Build Status](https://camo.githubusercontent.com/dcb2a2db33022c13e253d36e7f6e6e592e7953817e17f9152c9ff27a8bd3d34e/68747470733a2f2f7472617669732d63692e6f72672f776f64726f772f7969693277736f667464656c6574652e737667)](https://travis-ci.org/wodrow/yii2wsoftdelete)[![Latest Stable Version](https://camo.githubusercontent.com/34850c1047659036c74a60371b5075987e8a715b819e5d3e270363f752ad9f63/68747470733a2f2f706f7365722e707567782e6f72672f776f64726f772f7969693277736f667464656c6574652f762f737461626c652e737667)](https://packagist.org/packages/wodrow/yii2wsoftdelete)[![Total Downloads](https://camo.githubusercontent.com/d7cc06dbf6cc7d0df9f0160787dda2b645e79af507b1c0693dee8c8185de3e3b/68747470733a2f2f706f7365722e707567782e6f72672f776f64726f772f7969693277736f667464656c6574652f646f776e6c6f6164732e737667)](https://packagist.org/packages/wodrow/yii2wsoftdelete)[![Latest Unstable Version](https://camo.githubusercontent.com/95a04592793a34d3d3ec0e3c07a0b0e83132464a1cb4d7d354dd8340fac70fe6/68747470733a2f2f706f7365722e707567782e6f72672f776f64726f772f7969693277736f667464656c6574652f762f756e737461626c652e737667)](https://packagist.org/packages/wodrow/yii2wsoftdelete)[![License](https://camo.githubusercontent.com/f86fca9315757a7713ecef2d7fd1ddaaec6b658d385122365ca646d5cab9e368/68747470733a2f2f706f7365722e707567782e6f72672f776f64726f772f7969693277736f667464656c6574652f6c6963656e73652e737667)](https://packagist.org/packages/wodrow/yii2wsoftdelete)

Soft delete extension for Yii2 framework.

This extension ensures that soft-deleted has delete native consistent behavior and is IDE-friendly.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist wodrow/yii2wsoftdelete "^1.0.1"

```

or add

```
"wodrow/yii2wsoftdelete": "^1.0.1"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

Once the extension is installed, simply use it in your code by :

Edit model class:

```
use wodrow\softdelete\SoftDeleteBehavior;
use wodrow\softdelete\SoftDeleteTrait;

class Model extends \yii\db\ActiveRecord
{
    use SoftDeleteTrait;

    public static function getDeletedAtAttribute()
    {
        return "deleted_at";
    }

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = ArrayHelper::merge($behaviors, []);
        if (static::getDeletedAtAttribute()) {
            $behaviors = ArrayHelper::merge($behaviors, [
                'soft-delete' => [
                    'class' => SoftDeleteBehavior::class,
                    'deletedAtAttribute' => static::getDeletedAtAttribute(),
                ],
            ]);
        }
        return $behaviors;
    }
}
```

Change database table structures, add `deleted_at (int 11 unsigned)` field and attached to UNIQUE index.

API
---

[](#api)

### ActiveRecord class (SoftDelete Trait):

[](#activerecord-class-softdelete-trait)

find系列方法会返回 `wodrow\softdelete\ActiveQuery` 对象。

- softDelete() 使用软删除模式删除数据
- forceDelete() 使用物理删除模式强制删除数据
- restore() 恢复被软删除的模型数据
- isTrashed() 是否被软删除

以下命令分别是 `find()` / `findOne()` / `findAll()` 在不同模式下的对应版本：

所有模型（包括被软删除的）：

- findWithTrashed()
- findOneWithTrashed($condition)
- findAllWithTrashed($condition)

只查找被软删除的模型：

- findOnlyTrashed()
- findOneOnlyTrashed($condition)
- findAllOnlyTrashed($condition)

以下的命令均被重写成软删除版本：

- find()
- findOne()
- findAll()
- delete()

### wodrow\\softdelete\\ActiveQuery

[](#wodrowsoftdeleteactivequery)

增加了 `withTrashed()`, `withoutTrashed()` 和 `onlyTrashed()` 三个方法， 设置相应的查找模式。

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1274d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/919eb56724e4b3e9c2f88ba3d9d9ef817bfaaf47d705cc328b959f1c2f842976?d=identicon)[wodrow](/maintainers/wodrow)

---

Top Contributors

[![wodrow](https://avatars.githubusercontent.com/u/7815635?v=4)](https://github.com/wodrow "wodrow (8 commits)")

---

Tags

databaseyii2extensionsoftdelete

### Embed Badge

![Health badge](/badges/wodrow-yii2wsoftdelete/health.svg)

```
[![Health](https://phpackages.com/badges/wodrow-yii2wsoftdelete/health.svg)](https://phpackages.com/packages/wodrow-yii2wsoftdelete)
```

###  Alternatives

[johnitvn/yii2-ajaxcrud

Gii CRUD template for Single Page Ajax Administration for yii2

97209.6k16](/packages/johnitvn-yii2-ajaxcrud)[dmstr/yii2-db

Database extensions

19618.8k6](/packages/dmstr-yii2-db)[mg-code/yii2-helpers

A collection of useful helper classes for Yii framework 2.0

2022.5k5](/packages/mg-code-yii2-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
