没有不值得去解决的问题,也没有不值得去学习的技术!

在 Yii 2.0 上,声明与需验证模型特性相关的验证规则,使用了[[yii\behaviors\TimestampBehavior]],重写 [[yii\base\Model::rules()]] 方法的技巧

公共的模型逻辑层,验证规则方法调整如下
1、/common/models 目录中的模型类文件仅允许Gii工具所生成,为公共的模型数据层,如图1
/common/models 目录中的模型类文件仅允许Gii工具所生成,为公共的模型数据层
图1


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['group_id', 'config_column_id', 'plan_id', 'title', 'config_task_id', 'create_user_id', 'create_name', 'exec_user_id', 'exec_name', 'task_data', 'ended_at', 'created_at', 'updated_at'], 'required'],
            [['config_column_id', 'plan_id', 'sort_order', 'config_task_id', 'create_user_id', 'exec_user_id', 'ended_at', 'current_step_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['task_info', 'task_data'], 'string'],
            [['group_id', 'create_name', 'exec_name'], 'string', 'max' => 32],
            [['title'], 'string', 'max' => 64],
            [['place'], 'string', 'max' => 255],
        ];
    }


2、/common/logics 目录中的模型类文件为业务逻辑相关,继承至 /common/models 数据层,为公共的模型逻辑层,验证规则为场景相关的规则与公共的模型数据层的规则,在必填项中删除 created_at, updated_at


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            /* 指派(创建任务) */
            [ [ 'plan_id', 'title', 'config_task_id', 'exec_user_id'], 'required', 'on' => 'create' ], //create
            [ 'config_task_id', 'exist', 'targetClass' => '\common\logics\ConfigTask', 'targetAttribute' => 'id', 'filter' => [ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'plan_id', 'exist', 'targetClass' => '\common\logics\Plan', 'targetAttribute' => 'id', 'filter' => [ 'status' => Plan::PLAN_STATUS_PASSED ], 'on' => 'create' ], //create
            [ 'create_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'create_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ [ 'place', 'task_info' ], 'default', 'value' => '', 'on' => 'create' ], //create
            /* 我的任务(获取任务列表) */
            [ [ 'status' ], 'in', 'range' => [ self::PLAN_TASK_STATUS_ALL, self::PLAN_TASK_STATUS_NOT_BEGINNING, self::PLAN_TASK_STATUS_BEGINNING, self::PLAN_TASK_STATUS_DONE ], 'on' => 'index' ], //index
            [ [ 'config_task_id' ], 'in', 'range' => array_merge([ ConfigTask::CONFIG_TASK_STATUS_ALL ], ConfigTask::find()->select('id')->where([ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ])->asArray()->column()), 'on' => 'index' ], //index
            [ [ 'plan_id' ], 'default', 'value' => 0, 'on' => 'index' ], //index
            [ [ 'status', 'config_task_id' ], 'default', 'value' => self::PLAN_TASK_STATUS_ALL, 'on' => 'index' ], //index
            /* 认领(认领任务) */
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'targetAttribute' => [ 'id', 'exec_user_id' ], 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'claim' ], //claim
            /* 转派(转派任务) */
            [ [ 'exec_user_id' ], 'required', 'on' => 'transfer' ], //transfer
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'transfer' ], //transfer
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'transfer' ], //transfer
            [['group_id', 'config_column_id', 'plan_id', 'title', 'config_task_id', 'create_user_id', 'create_name', 'exec_user_id', 'exec_name', 'task_data', 'ended_at'], 'required'],
            [['config_column_id', 'plan_id', 'sort_order', 'config_task_id', 'create_user_id', 'exec_user_id', 'ended_at', 'current_step_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['task_info', 'task_data'], 'string'],
            [['group_id', 'create_name', 'exec_name'], 'string', 'max' => 32],
            [['title'], 'string', 'max' => 64],
            [['place'], 'string', 'max' => 255],
        ];


3、公共的模型逻辑层,验证规则方法调整如下,如图2
公共的模型逻辑层,验证规则方法调整如下
图2



use yii\helpers\ArrayHelper;

class PlanTask extends \common\models\PlanTask
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        $rules = [
            /* 指派(创建任务) */
            [ [ 'plan_id', 'title', 'config_task_id', 'exec_user_id'], 'required', 'on' => 'create' ], //create
            [ 'config_task_id', 'exist', 'targetClass' => '\common\logics\ConfigTask', 'targetAttribute' => 'id', 'filter' => [ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'plan_id', 'exist', 'targetClass' => '\common\logics\Plan', 'targetAttribute' => 'id', 'filter' => [ 'status' => Plan::PLAN_STATUS_PASSED ], 'on' => 'create' ], //create
            [ 'create_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'create_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ [ 'place', 'task_info' ], 'default', 'value' => '', 'on' => 'create' ], //create
            /* 我的任务(获取任务列表) */
            [ [ 'status' ], 'in', 'range' => [ self::PLAN_TASK_STATUS_ALL, self::PLAN_TASK_STATUS_NOT_BEGINNING, self::PLAN_TASK_STATUS_BEGINNING, self::PLAN_TASK_STATUS_DONE ], 'on' => 'index' ], //index
            [ [ 'config_task_id' ], 'in', 'range' => array_merge([ ConfigTask::CONFIG_TASK_STATUS_ALL ], ConfigTask::find()->select('id')->where([ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ])->asArray()->column()), 'on' => 'index' ], //index
            [ [ 'plan_id' ], 'default', 'value' => 0, 'on' => 'index' ], //index
            [ [ 'status', 'config_task_id' ], 'default', 'value' => self::PLAN_TASK_STATUS_ALL, 'on' => 'index' ], //index
            /* 认领(认领任务) */
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'targetAttribute' => [ 'id', 'exec_user_id' ], 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'claim' ], //claim
            /* 转派(转派任务) */
            [ [ 'exec_user_id' ], 'required', 'on' => 'transfer' ], //transfer
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'transfer' ], //transfer
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'transfer' ], //transfer
        ];
        $parentRules = parent::rules();
        return ArrayHelper::merge($rules, $parentRules);
    }
}


4、由于使用了 [[yii\behaviors\TimestampBehavior]],这个行为支持在 [[yii\db\ActiveRecord|Active Record]] 存储时自动更新它的时间戳属性,因此需要在验证规则中去掉:created_at, updated_at,设置其默认值为0,如图3
由于使用了 [[yii\behaviors\TimestampBehavior]],这个行为支持在 [[yii\db\ActiveRecord|Active Record]] 存储时自动更新它的时间戳属性,因此需要在验证规则中去掉:created_at, updated_at,设置其默认值为0
图3
5、/common/models 目录中的模型类文件仅允许Gii工具所生成,为公共的模型数据层,生成的验证规则:created_at, updated_at,已经不是必填项


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['group_id', 'config_column_id', 'plan_id', 'title', 'config_task_id', 'create_user_id', 'create_name', 'exec_user_id', 'exec_name', 'task_data', 'ended_at'], 'required'],
            [['config_column_id', 'plan_id', 'sort_order', 'config_task_id', 'create_user_id', 'exec_user_id', 'ended_at', 'current_step_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['task_info', 'task_data'], 'string'],
            [['group_id', 'create_name', 'exec_name'], 'string', 'max' => 32],
            [['title'], 'string', 'max' => 64],
            [['place'], 'string', 'max' => 255],
        ];
    }


PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理