In Yii 2.0, the core validator: UNIQUE (unique) supports implementation of multiple models and variables
1. The article involving the 1st table, the Penguin number: cpa_qq_article, the field needs to be verified: the uniqueness of the title, the structure of the table is shown in Figure 1
2. The task of the application of the channel involved in the second table, the application task of the channel: CPA_CHANNEL_APP_TASK, field: task_id and the field of the penguin number: task_id There is an association relationship (many-to-one), field: channel_app_source_uuid is a condition (variable, request parameter), field: status is a condition (constant), the table structure is shown in Figure 2
3. Filter: used to check the uniqueness of the input value, and the database query is bound to be performed, and this attribute is a filter condition for further filtering the query. An anonymous function with the style function ($query), the $query parameter is the query object you want to modify within the function. The validation rules in the model are declared as follows:
/**
* {@inheritdoc}
*/
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_CREATE] = ['group_id', 'article_category_id', 'title', 'author', 'source_article_id'];
return $scenarios;
}
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
['title', 'unique', 'filter' => function ($query) {
/* @var $query ChannelAppTaskQuery */
$query->joinWith('channelAppTask', false)->andWhere([ChannelAppTask::tableName() . '.status' => ChannelAppTask::STATUS_PLATFORM_PUBLISHED]);
}, 'on' => self::SCENARIO_CREATE],
];
$parentRules = parent::rules();
return ArrayHelper::merge($rules, $parentRules);
}
/*
* 声明了一个渠道的应用的任务关联
*/
public function getChannelAppTask()
{
return $this->hasOne(ChannelAppTask::className(), ['task_id' => 'task_id']);
}
When verifying the request parameters, the generated SQL is as follows:
SELECT EXISTS(SELECT `cpa_qq_article`.* FROM `cpa_qq_article` LEFT JOIN `cpa_channel_app_task` ON `cpa_qq_article`.`task_id` = `cpa_channel_app_task`.`task_id` WHERE (`cpa_qq_article`.`title`='未来 10 年,“星光中国芯工程”计划投资 100 亿元用于芯片研发及大规模产业化。') AND (`cpa_channel_app_task`.`status`=6))
5. In the request parameter, there is a channel_app_source_uuids field, and its value is an array:[“8d72b7cc2ac911eab85a54ee75d2ebc1”, “18cf06d22ac911eaa31854ee75d2ebc1”], you need to apply the value of channel_app_source_uuids to the query condition of uniqueness validation as a condition. Prior to executing the model validation, the value is assigned to the applied global parameter variable yii::$app->params. as shown in Figure 3
$requestParams = Yii::$app->getRequest()->getBodyParams();
Yii::$app->params['channelAppSourceUuids'] = $requestParams['channel_app_source_uuids'];
6. You can now use yii::$app->params in the validation rules. The verification rule declaration in the model is adjusted as follows:
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
['title', 'unique', 'filter' => function ($query) {
/* @var $query ChannelAppTaskQuery */
$query->joinWith('channelAppTask', false)->andWhere(['and', ['in', ChannelAppTask::tableName() . '.channel_app_source_uuid', Yii::$app->params['channelAppSourceUuids']], [ChannelAppTask::tableName() . '.status' => ChannelAppTask::STATUS_PLATFORM_PUBLISHED]]);
}, 'on' => self::SCENARIO_CREATE],
];
$parentRules = parent::rules();
return ArrayHelper::merge($rules, $parentRules);
}
7. When verifying the request parameters, the generated SQL is as follows, the request parameters have been applied to the query conditions, which is in line with the expected
SELECT EXISTS(SELECT `cpa_qq_article`.* FROM `cpa_qq_article` LEFT JOIN `cpa_channel_app_task` ON `cpa_qq_article`.`task_id` = `cpa_channel_app_task`.`task_id` WHERE (`cpa_qq_article`.`title`='未来 10 年,“星光中国芯工程”计划投资 100 亿元用于芯片研发及大规模产业化。') AND ((`cpa_channel_app_task`.`channel_app_source_uuid` IN ('8d72b7cc2ac911eab85a54ee75d2ebc1', '18cf06d22ac911eaa31854ee75d2ebc1')) AND (`cpa_channel_app_task`.`status`=6)))
8. Core validator: unique (unique) supports the implementation of multiple models and variables. When the verification fails, the response is as follows, as shown in Figure 4
{
"code": 226004,
"message": "数据验证失败:标题的值\"未来 10 年,“星光中国芯工程”计划投资 100 亿元用于芯片研发及大规模产业化。\"已经被占用了。"
}


![请求参数中,存在 channel_app_source_uuids 字段,其值为数组:["8d72b7cc2ac911eab85a54ee75d2ebc1", "18cf06d22ac911eaa31854ee75d2ebc1"],需要将 channel_app_source_uuids 的值做为条件应用于唯一性验证的查询条件中。在执行模型的验证之前,赋值给应用的全局参数变量 Yii::$app->params。](https://www.shuijingwanwq.com/wp-content/uploads/2020/02/3.png)
