In Yii 2, when the input data is through the URL, in order to avoid redundant update SQL, apply a filter (Intval) to the input value, which in turn leads to the analysis of the invalidation of the derived verification rule.
1. Open the URL in the browser:http://www.channel-pub-api-localhost.chinamcloud.com/weibo-oauth2/authorize-update?group_id=spider&channel_app_source_uuid=269f68bc098011e9b1c354ee75d2ebc1&user_name=华栖云1658397962&permission=4&status=1&redirect_uri=aHR0cDovL3d3dy56bXQuY29tLw%3D%3D, at this time the verification rule takes effect, as shown in Figure 1
2. The verification rules are: Permission: optional, permission, 1: synchronization; 2: release; 3: synchronization and release
/* 更新微博的微连接的网页应用的用户 */
[['permission'], 'in', 'range' => [self::PERMISSION_SYNC, self::PERMISSION_PUB, self::PERMISSION_SYNC_PUB], 'on' => self::SCENARIO_UPDATE],
3. At this time, the value of Permission in the database is 2, and the value of updated_at is 1545961613, as shown in Figure 2
4. Open the URL in the browser:http://www.channel-pub-api-localhost.chinamcloud.com/weibo-oauth2/authorize-update?group_id=spider&channel_app_source_uuid=269f68bc098011e9b1c354ee75d2ebc1&user_name=华栖云1658397962&permission=2&status=1&redirect_uri=aHR0cDovL3d3dy56bXQuY29tLw%3D%3D, the value of Permission is 2, since the Permission field is of type SMALLINT(6), and the status field is SMALLINT(6) type, and the parameter passed through the URL is the string type, so even if The value of Permission and Status has not changed, and the update SQL is still executed, and the SQL statement is executed as follows:
UPDATE `cpa_weibo_weibo_connect_web_app_user` SET `permission`=2, `status`=1, `updated_at`=1546066986 WHERE `id`=17
5. Adjust the verification rules and apply a filter (Intval) to the input value
/* 更新微博的微连接的网页应用的用户 */
[['permission'], 'in', 'range' => [self::PERMISSION_SYNC, self::PERMISSION_PUB, self::PERMISSION_SYNC_PUB], 'on' => self::SCENARIO_UPDATE],
[['permission', 'status'], 'filter', 'filter' => 'intval', 'on' => self::SCENARIO_UPDATE],
6. Open the URL in the browser:http://www.channel-pub-api-localhost.chinamcloud.com/weibo-oauth2/authorize-update?group_id=spider&channel_app_source_uuid=269f68bc098011e9b1c354ee75d2ebc1&user_name=华栖云1658397962&permission=&status=1&redirect_uri=aHR0cDovL3d3dy56bXQuY29tLw%3D%3D, the value of Permission is an empty string. At this time, the verification rule is invalid, and the value of Permission is updated to 0, and the SQL statement is executed as follows:
UPDATE `cpa_weibo_weibo_connect_web_app_user` SET `permission`=0, `updated_at`=1546067544 WHERE `id`=17
7. Adjust the verification rules and put the filter (Intval) at the forefront
/* 更新微博的微连接的网页应用的用户 */
[['permission', 'status'], 'filter', 'filter' => 'intval', 'on' => self::SCENARIO_UPDATE],
[['permission'], 'in', 'range' => [self::PERMISSION_SYNC, self::PERMISSION_PUB, self::PERMISSION_SYNC_PUB], 'on' => self::SCENARIO_UPDATE],
8. Open the URL in the browser:http://www.channel-pub-api-localhost.chinamcloud.com/weibo-oauth2/authorize-update?group_id=spider&channel_app_source_uuid=269f68bc098011e9b1c354ee75d2ebc1&user_name=华栖云1658397962&permission=&status=1&redirect_uri=aHR0cDovL3d3dy56bXQuY29tLw%3D%3D, the value of Permission is an empty string, and the verification rule takes effect at this time, but its value has changed from the string to 0. It is expected that when its value is not “null value”, the integer value is converted, as shown in Figure 3
9. By default, when the input item is an empty string, an empty array, or null, it will be regarded as “null value”. In the Core Validator, the Filter (Filter) validator handles empty input by default. Adjust the Filter (filter) validator, its yii\base\validator::skiponeempty attribute is false, and it is adjusted to true
/* 更新微博的微连接的网页应用的用户 */
[['permission', 'status'], 'filter', 'filter' => 'intval', 'skipOnEmpty' => true, 'on' => self::SCENARIO_UPDATE],
[['permission'], 'in', 'range' => [self::PERMISSION_SYNC, self::PERMISSION_PUB, self::PERMISSION_SYNC_PUB], 'on' => self::SCENARIO_UPDATE],
10. Open the URL in the browser:http://www.channel-pub-api-localhost.chinamcloud.com/weibo-oauth2/authorize-update?group_id=spider&channel_app_source_uuid=269f68bc098011e9b1c354ee75d2ebc1&user_name=华栖云1658397962&permission=&status=1&redirect_uri=aHR0cDovL3d3dy56bXQuY29tLw%3D%3D, the value of Permission is an empty string, at this time, the verification rule takes effect, and the data validation fails: the permission cannot be empty. as shown in Figure 4
11. At this time, the order of the 2 rules can be adjusted at will, because the two rules are only executed when the value is not “null value”, and the recommendation is placed at the end, indicating that when the verification is passed, it will be converted to an integer value.
/* 更新微博的微连接的网页应用的用户 */
[['permission'], 'in', 'range' => [self::PERMISSION_SYNC, self::PERMISSION_PUB, self::PERMISSION_SYNC_PUB], 'on' => self::SCENARIO_UPDATE],
[['permission', 'status'], 'filter', 'filter' => 'intval', 'skipOnEmpty' => true, 'on' => self::SCENARIO_UPDATE],
12. Open the URL in the browser:http://www.channel-pub-api-localhost.chinamcloud.com/weibo-oauth2/authorize-update?group_id=spider&channel_app_source_uuid=269f68bc098011e9b1c354ee75d2ebc1&user_name=华栖云1658397962&permission=&status=1&redirect_uri=aHR0cDovL3d3dy56bXQuY29tLw%3D%3D, the value of Permission is an empty string, at this time, the verification rule takes effect, and the data validation fails: the permission cannot be empty. Consistent with the exception response in step 10, as expected



