On Yii 2.0, declare the validation rules related to the characteristics of the model that need to be validated, using[[yii\behaviors\TimestampBehavior]], rewrite[[yii\base\Model::rules()]] method of method
1. The model class file in the /common/models directory is only allowed to be generated by the GII tool, which is a common model data layer, as shown in Figure 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. The model file in the /common/logics directory is related to business logic, which is inherited to /common/models The data layer is the common model logic layer, the verification rule is the scene-related rules and the public model data layer rules, and delete the created_at, updated_at in the required fields
/**
* @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. The common model logic layer, the verification rules and methods are adjusted as follows, as shown in Figure 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. Due to the use of[[yii\behaviors\TimestampBehavior]], this behavior is supported in[[yii\db\ActiveRecord|Active Record]] It automatically updates its timestamp attribute during storage, so it needs to be removed in the validation rule: created_at, updated_at, and its default value is 0, as shown in Figure 3
5. The model class file in the /common/models directory is only allowed to be generated by the GII tool. It is a common model data layer.
/**
* {@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],
];
}


![由于使用了 [[yii\behaviors\TimestampBehavior]],这个行为支持在 [[yii\db\ActiveRecord|Active Record]] 存储时自动更新它的时间戳属性,因此需要在验证规则中去掉:created_at, updated_at,设置其默认值为0](https://www.shuijingwanwq.com/wp-content/uploads/2018/05/3-4.png)