• YII 优雅的实现软删


    大部分项目都有软删的需求,我们现项目也自己封装了一个软删除的类,但是存在不够健壮,查询数据不友好等缺陷。
    原来软删除Trait 类代码;

    
    
    namespace app\common\trait_class;
    
    
    /**
     * 软删除工具类
     * Trait SoftDeleteTrait
     * @package app\common\trait_class
     */
    trait SoftDeleteTrait
    {
        /**
         * @var int 删除默认值
         */
        static $deleteDefault = 0;
    
        /**
         * 软删除
         * @return mixed
         * @throws \Exception
         */
        public function softDelete()
        {
            $this->{self::getDeletedAtAttribute()} = time();
            $ret = $this->save(false, [self::getDeletedAtAttribute()]);
            $this->afterSoftDelete();
            return $ret;
        }
    
        public function afterSoftDelete()
        {
            // Default implementation
        }
    
        /**
         * 软删除
         * @return mixed
         * @throws \Exception
         */
        public function delete()
        {
            return $this->softDelete();
        }
    
        /**
         * Gets the deleted_at attribute name
         *
         * @throws \Exception
         */
        static public function getDeletedAtAttribute()
        {
            return 'deleted_at';
        }
    
        /**
         * @return mixed
         * @throws \Exception
         */
        public function restore()
        {
            $this->{self::getDeletedAtAttribute()} = self::$deleteDefault;
            return $this->save(false, [self::getDeletedAtAttribute()]);
        }
    
    
        /**
         * 批量删除
         * @param null $condition
         * @param array $params
         * @return mixed
         * @throws \Exception
         */
        public static function deleteAll($condition = null, $params = [])
        {
            $deleteField = self::getDeletedAtAttribute();
            $deleteFieldValue["{$deleteField}"] = time();
    
            $command = static::getDb()->createCommand();
            $command->update(static::tableName(), $deleteFieldValue, $condition,$params);
            return $command->execute();
        }
    
        /**
         * 过滤软删除的数据
         * @return \yii\db\ActiveQuery
         * @throws \Exception
         */
        public static function find()
        {
            $deleteField = self::getDeletedAtAttribute();
            $where = [
                static::tableName() . '.' . "{$deleteField}" => self::$deleteDefault
            ];
            return parent::find()->andWhere($where);
        }
    
        /**
         * 查询包含软删除的数据
         * @return \yii\db\ActiveQuery
         * @throws \Exception
         */
        public static function withTrashedFind()
        {
            return parent::find();
        }
    
        /**
         * 只查询软删除数据
         * @return \yii\db\ActiveQuery
         * @throws \Exception
         */
        public static function onlyTrashedFind()
        {
            $deleteField = self::getDeletedAtAttribute();
            $where = ['not', [$deleteField => self::$deleteDefault]];
            return parent::find()->andWhere($where);
        }
    
        /**
         * 判断当前实例是否被软删除
         * @return false
         * @throws \Exception
         */
        public function trashed()
        {
            $field = $this->getDeletedAtAttribute();
            $softDelete = $this->$field ?? false;
            if ($field && ($softDelete === self::$deleteDefault)) {
                return false;
            }
            return true;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    以上代码为自己封装,好处是用Trait,可以随时声明软删,实现方便。但是存在一些问题如不够健壮,如只默认deleted_at 为删除字段,默认值为0等,并且对于数据查询不够友好等缺陷
    以下是结合第三方包及Trait的实现,不仅具备方便而且安全健壮

    引入第三方库
    composer require yii2tech/ar-softdelete
    
    • 1

    修改Trait类

    
    
    namespace app\common\trait_class;
    
    use yii2tech\ar\softdelete\SoftDeleteBehavior;
    use yii2tech\ar\softdelete\SoftDeleteQueryBehavior;
    
    /**
     * 软删除工具类
     * Trait SoftDeleteTrait
     * @package app\common\trait_class
     */
    trait SoftDeleteTrait
    {
        public function behaviors()
        {
            return [
                'softDeleteBehavior' => [
                    'class' => SoftDeleteBehavior::class,
                    'softDeleteAttributeValues' => [
                        'deleted_at' => function ($model) {
                            return time();
                        }
                    ],
                    'restoreAttributeValues' => [
                        'deleted_at' => 0
                    ]
                ],
            ];
        }
    
        /**
         * @return \yii\db\ActiveQuery|SoftDeleteQueryBehavior
         */
        public static function find()
        {
            $query = parent::find();
            $query->attachBehavior('softDelete', SoftDeleteQueryBehavior::class);
            return $query;
        }
    
    	/**
    	 * 声明软删则不能批量删除
         * @param null $condition
         * @param array $params
         * @throws Exception
         */
        public static function deleteAll($condition = null, $params = [])
        {
            throw new Exception("软删不支持批量删除");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    因为这个第三类库的deleted()函数的删除时间默认为softDeleteAttributeValues的值,所以需要重写deleted()方法;如在Users中
    重写Users 的find()方法使其能调用User 的Query Scope 类

    class users extends \yii\db\ActiveRecord
    {
        /**
         * @return GoodsGroup|object|\yii\db\ActiveQuery
         * @throws \yii\base\InvalidConfigException
         */
        public static function find()
        {
            return new UserQuery(get_called_class());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    User Query Scope

    class UserQuery extends ActiveQuery
    {
    	
        /**
         * 重写覆盖Soft delete 的deleted()
         * @return GoodsGroup
         */
        public function deleted()
        {
            return $this->andFilterWhere(['>', 'deleted_at', 0]);
        }
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在仓库层use SoftDeleteTrait;实现软删

    总结

    1. 应用的第三类库是Behavior 行为实现的经典应用。
    2. 熟知当前类,Trait,父类库的方法调用覆盖顺序,并类库的deleted()函数重写
    3. 需要注意的是User需要新增Behavior附加行为的时候注意Behavior()会被重写
  • 相关阅读:
    拓端tecdat|python在Scikit-learn中用决策树和随机森林预测NBA获胜者
    用DIV+CSS技术制作个人博客网站(web前端网页制作课期末作业)
    三分钟数据持久化:Spring Boot, JPA 与 SQLite 的完美融合
    T - SQL使用事务 及 在Winform使用事务
    vscode electron安装环境
    【C刷题】day6
    Unity URP 如何写基础的曲面细分着色器
    软件机器人助力企业产地证自动化申报,提高竞争力,降低成本
    Rust的协程机制:原理与简单示例
    Java数据结构之二叉树的构建与遍历
  • 原文地址:https://blog.csdn.net/qq_39941141/article/details/127907202