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

How to Override [[yii\base\Model::rules()]] in Yii 2.0 to Declare Validation Rules for Model Attributes Using [[yii\behaviors\TimestampBehavior]]

In the common model logic layer, the validation rules method is adjusted as follows

1. The model class files in the /common/models directory are only generated by the Gii tool and serve as the common model data layer, as shown in Figure 1.

The model class files in the /common/models directory are only generated by the Gii tool and serve as the common model data layer
Figure 1
PHP

    /**
     * {@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. The model class files in the /common/logics directory pertain to business logic. They inherit from the /common/models data layer and serve as the common model logic layer. The validation rules consist of scenario-specific rules and the rules from the common model data layer, with created_at and updated_at removed from the required fields.

PHP

    /**
     * @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. In the common model logic layer, the validation rules method is adjusted as follows, as shown in Figure 2.

In the common model logic layer, the validation rules method is adjusted as follows
Figure 2
PHP


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. Because [[yii\behaviors\TimestampBehavior]] is used, and this behavior supports automatically updating its timestamp attributes when saving the [[yii\db\ActiveRecord|Active Record]], you need to remove created_at and updated_at from the validation rules and set their default values to 0, as shown in Figure 3.

Because [[yii\behaviors\TimestampBehavior]] is used, and this behavior supports automatically updating its timestamp attributes when saving the [[yii\db\ActiveRecord|Active Record]], you need to remove created_at and updated_at from the validation rules and set their default values to 0
Figure 3

5. The model class files in the /common/models directory are only generated by the Gii tool and serve as the common model data layer. In the generated validation rules, created_at and updated_at are no longer required fields.

PHP

    /**
     * {@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],
        ];
    }

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系