Data validation fails in Yii 2.0: the source publishing user ID must be an analysis of a string (to ensure the compatibility of the version, both numeric type and string type must be supported)

在 Yii 2.0 中数据验证失败:来源发布用户ID必须是一条字符串。
1. Data validation failed in Yii 2.0: the source publishing user ID must be a string. as shown in Figure 1
在 Yii 2.0 中数据验证失败:来源发布用户ID必须是一条字符串。
Figure 1
2. The reason is the field: source_pub_user_id was int(11) in the table before, and later modified to varchar(64), in the model file regenerated based on GII, the verification rules are as follows


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['source_pub_user_id'], 'string', 'max' => 64],
        ];
    }


3. The interface has been connected to multiple clients. In order to ensure the compatibility of the version, the client cannot be forced to adjust the type of the field to be a string type, and it still needs to be compatible with the numeric type. Therefore, in GII Before the generated validation rules, execute the filter first, convert it to a string type, and adjust the verification rules as follows


    /**
     * @inheritdoc
     */
    public function rules()
    {
        $rules = [
            /* 创建 */
            // 标准化 "source_pub_user_id" 输入
            ['source_pub_user_id', 'filter', 'filter' => function ($value) {
                // 在此处标准化输入的来源发布用户ID
                return (string) $value;
            }, 'on' => self::SCENARIO_CREATE],
        ];
        $parentRules = parent::rules();

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


4. The data verification is successful in Yii 2.0, as shown in Figure 2
在 Yii 2.0 中数据验证成功
Figure 2
5. Check the generated SQL statement, field: source_pub_user_id is already a string type, as shown in Figure 3
查看生成的 SQL 语句,字段:source_pub_user_id 已经是字符串类型
Figure 3


INSERT INTO `cpa_task` (`group_id`, `source`, `source_uuid`, `source_pub_user_id`, `source_callback_url`, `channel_id`, `channel_code`, `channel_type_id`, `channel_type_code`, `status`, `created_at`, `updated_at`) VALUES ('015ce30b116ce86058fa6ab4fea4ac63', 'spider', '825e6d5e36468cc4bf536799ce3565cf', '3', 'http://scms.wjdev.chinamcloud.cn/api/thirdPush/callBack', 1, 'qq', 1, 'qq_cw', 1, 1566280998, 1566280998)


 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.