PHPackages                             liuyuanjun/yii2-softdelete - 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. liuyuanjun/yii2-softdelete

ActiveLibrary[Database &amp; ORM](/categories/database)

liuyuanjun/yii2-softdelete
==========================

Soft delete trait for Yii2.

v1.1.2(2y ago)11361MITPHPPHP &gt;=7.0

Since Sep 2Pushed 2y ago1 watchersCompare

[ Source](https://github.com/liuyuanjun/yii2-softdelete)[ Packagist](https://packagist.org/packages/liuyuanjun/yii2-softdelete)[ RSS](/packages/liuyuanjun-yii2-softdelete/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (8)Used By (1)

Yii2 软删 yii2-softdelete
=======================

[](#yii2-软删-yii2-softdelete)

Soft delete trait for Yii2.

- 替换了 yii\\db\\ActiveRecord 的 delete() 方法，软删除时将 is\_deleted 字段设置为主键字段的值，而不是删除数据，从而避免了使用删除时间等来标记软删时唯一索引可能产生冲突的问题(设置唯一索引需要将 is\_deleted 字段加入，作联合唯一索引)
- 使用forceDelete()方法可硬删除数据
- withTrashed() 方法可在查询时包含软删数据 onlyTrashed() 方法可在查询时只包含软删数据
- 可正常使用Model的 find、delete、save、hasOne、hasMany、via 等方法

安装
--

[](#安装)

`composer require liuyuanjun/yii2-softdelete`

用法 Usage
--------

[](#用法-usage)

### 在Model类中引入

[](#在model类中引入)

```
    use liuyuanjun\yii2\softdelete\SoftDeleteTrait;

    /*
    ⚠️ 注意 Attention:
    数据表主键类型只能为 int 或者 string ！'is_deleted'字段类型与主键相同且默认值设置为 0 或 空字符串。
    */
    class Model extends \Yii\db\ActiveRecord
    {
        use SoftDeleteTrait;

        /**
         * is_deleted column name
         * The default value is 'is_deleted', if you want to change it overwrite this method.
         * @return string
         * @author Yuanjun.Liu
        */
        public static function getIsDeletedAttribute(): string
        {
            return 'is_deleted';
        }
    }

    //+++++ OR +++++

    class Model2 extends \liuyuanjun\yii2\softdelete\SoftDeleteActiveRecord
    {
        public static function getIsDeletedAttribute(): string
        {
            return 'is_deleted';
        }
    }
```

\*\* 然后像往常一样正常使用 \*\*

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity50

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 ~138 days

Recently: every ~204 days

Total

7

Last Release

879d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/22c3c1b4130eec2c5a30446f9e7396bfa4ad2e172db7791bf132c493b5ae2795?d=identicon)[liuyuanjun](/maintainers/liuyuanjun)

---

Top Contributors

[![liuyuanjun](https://avatars.githubusercontent.com/u/1127718?v=4)](https://github.com/liuyuanjun "liuyuanjun (18 commits)")

### Embed Badge

![Health badge](/badges/liuyuanjun-yii2-softdelete/health.svg)

```
[![Health](https://phpackages.com/badges/liuyuanjun-yii2-softdelete/health.svg)](https://phpackages.com/packages/liuyuanjun-yii2-softdelete)
```

###  Alternatives

[yii2tech/illuminate

Yii2 to Laravel Migration Package

11315.1k](/packages/yii2tech-illuminate)[mootensai/yii2-relation-trait

Yii 2 Models load with relation, &amp; transaction save with relation

47220.3k9](/packages/mootensai-yii2-relation-trait)[nhkey/yii2-activerecord-history

Storage history of changes to ActiveRecord

46143.4k1](/packages/nhkey-yii2-activerecord-history)[dmstr/yii2-db

Database extensions

19618.8k6](/packages/dmstr-yii2-db)[urbanindo/yii2-dynamodb

DynamoDB extensions for Yii2

1471.4k](/packages/urbanindo-yii2-dynamodb)[spanjeta/yii2-backup

Database Backup and Restore functionality

285.0k1](/packages/spanjeta-yii2-backup)

PHPackages © 2026

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