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

Analyzing and Resolving Validation Rule Failures in Yii 2 When Applying intval to URL Inputs to Avoid Redundant Update SQL

At this point, the value of permission in the database is 2, and the value of updated_at is 1545961613

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 point, the validation rules take effect, as shown in Figure 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 point, the validation rules take effect
Figure 1

2. The validation rule is: permission: optional, permission, 1: sync; 2: publish; 3: sync and publish

PHP

            /* 更新微博的微连接的网页应用的用户 */
            [['permission'], 'in', 'range' => [self::PERMISSION_SYNC, self::PERMISSION_PUB, self::PERMISSION_SYNC_PUB], 'on' => self::SCENARIO_UPDATE],

3. At this point, the value of permission in the database is 2, and the value of updated_at is 1545961613, as shown in Figure 2.

At this point, the value of permission in the database is 2, and the value of updated_at is 1545961613
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. Because the permission field is of type SMALLINT(6) and the status field is of type SMALLINT(6), while the parameters passed through the URL are of string type, an update SQL is executed even though the values of permission and status have not changed. The executed SQL statement is as follows:

SQL

UPDATE `cpa_weibo_weibo_connect_web_app_user` SET `permission`=2, `status`=1, `updated_at`=1546066986 WHERE `id`=17

5. Adjust the validation rules to apply a filter (intval) to the input value.

PHP

            /* 更新微博的微连接的网页应用的用户 */
            [['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 point, the validation rules fail, the value of permission is updated to 0, and the executed SQL statement is as follows:

SQL

UPDATE `cpa_weibo_weibo_connect_web_app_user` SET `permission`=0, `updated_at`=1546067544 WHERE `id`=17

7. Adjust the validation rules to place the filter (intval) first.

PHP

            /* 更新微博的微连接的网页应用的用户 */
			[['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. At this point, the validation rules take effect, but its value has already changed from a string to 0. The expectation is to only convert the integer value when it is not an “empty value”, as shown in Figure 3.

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 point, the validation rules take effect, but its value has already changed from a string to 0. The expectation is to only convert the integer value when it is not an "empty value"
Figure 3

9. By default, when an input is an empty string, an empty array, or null, it is considered an “empty value”. In the core validators, the filter validator processes empty input by default. Adjust the filter validator by changing its yii\base\Validator::skipOnEmpty property from false to true.

PHP

            /* 更新微博的微连接的网页应用的用户 */
			[['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 point, the validation rules take effect, and data validation fails: Permission cannot be blank. As shown in Figure 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=&status=1&redirect_uri=aHR0cDovL3d3dy56bXQuY29tLw%3D%3D . The value of permission is an empty string. At this point, the validation rules take effect, and data validation fails: Permission cannot be blank.
Figure 4

11. At this point, the order of the two rules can be adjusted freely, because both rules only execute validation when their values are not “empty values”. It is recommended to place it at the end, indicating that the integer value conversion occurs only after validation passes.

PHP

            /* 更新微博的微连接的网页应用的用户 */
            [['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 point, the validation rules take effect, and data validation fails: Permission cannot be blank. This is consistent with the error response in step 10 and meets expectations.

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

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

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

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

关于我  ·  GitHub  ·  邮件联系