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)
1. Data validation failed in Yii 2.0: the source publishing user ID must be a string. as shown in 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]#ATFP_CLOSE_Translate_span#,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 =[
/* create */
// normalize "source_pub_user_id" input
[SOURCE_PUB_USER_ID,filter,filter=> function ($value) {
// Standardize the input source to publish the user ID here
return (string) $value;
},on=> self::scenario_create]#atfp_close_translate_span#,
];
$parentrules = parent::rules();
return ArrayHelper::merge($rules, $parentrules);
}
4. The data verification is successful in Yii 2.0, as shown in Figure 2
5. Check the generated SQL statement, field: source_pub_user_id is already a string type, as shown in 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)


