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

In Yii 2, How to Skip Model Updates When a Field Is Absent or Empty

When making a PUT request to http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps/148d4df6eba311e899f654ee75d2ebc1?group_id=spider , the response succeeds

1. GET http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps/edit/148d4df6eba311e899f654ee75d2ebc1?group_id=spider . The response is as follows. For security reasons, the Client Secret is reset to an empty string.

JSON

{
    "code": 10000,
    "message": "编辑企鹅号的内容网站应用成功",
    "data": {
        "channel_app_source_uuid": "148d4df6eba311e899f654ee75d2ebc1",
        "penguin_name": "篮球场马达1",
        "penguin_login_qq": "3176058386",
        "penguin_login_wx": "",
        "client_id": "32deed1727f9a23a504d6dda48938de0",
        "client_secret": "",
        "permission": 2,
        "status": 0
    }
}

2. Therefore, when making a PUT request to http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps/148d4df6eba311e899f654ee75d2ebc1?group_id=spider , if the value of client_secret is not updated, its value is an empty string.

Body

JSON

{
	"penguin_name": "篮球场马达1",
	"penguin_login_qq": "3176058386",
	"penguin_login_wx": "",
	"client_id": "32deed1727f9a23a504d6dda48938de0",
	"client_secret": "",
	"permission": 2,
	"status": 0
}

3. The request response fails with a 422 status code, as shown in Figure 1.

Request response failed, 422
Figure 1
JSON

{
    "code": 20004,
    "message": "数据验证失败:Client Secret不能为空。"
}

4. Now I need to implement logic so that when the value of client_secret is an empty string, validation is not performed on client_secret and its value is not updated. I edit the validation rules.

PHP

    /**
     * @inheritdoc
     */
    public function rules()
    {
        $rules = [
            /* 更新企鹅号的内容网站应用 */
            [['client_secret'], 'validateClientSecret', 'skipOnEmpty' => false, 'on' => self::SCENARIO_UPDATE],
            [['permission'], 'in', 'range' => [self::PERMISSION_SYNC, self::PERMISSION_PUB, self::PERMISSION_SYNC_PUB], 'on' => self::SCENARIO_UPDATE],
            [['status'], 'in', 'range' => [self::STATUS_DISABLED, self::STATUS_ENABLED], 'on' => self::SCENARIO_UPDATE],
        ];
        $parentRules = parent::rules();

        return ArrayHelper::merge($rules, $parentRules);
    }

    /**
     * Validates the Client Secret.
     * This method serves as the inline validation for Client Secret.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateClientSecret($attribute, $params)
    {
        // 当 Client Secret 为空时,赋值 Client Secret 为原先的值
        if (empty($this->$attribute)) {
            $this->$attribute = $this->getOldAttribute($attribute);
        }
    }

5. When making a PUT request to http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps/148d4df6eba311e899f654ee75d2ebc1?group_id=spider , the response succeeds, as shown in Figure 2.

When making a PUT request to http://api.channel-pub-api.localhost/qq/v1/qq-cw-apps/148d4df6eba311e899f654ee75d2ebc1?group_id=spider , the response succeeds
Figure 2

6. Checking the SQL statement, it meets expectations: when the value of client_secret is an empty string, the value of client_secret is not updated.

SQL

UPDATE `cpa_qq_cw_app` SET `permission`=1, `updated_at`=1542610618 WHERE `id`=6

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

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

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

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

关于我  ·  GitHub  ·  邮件联系