Based on yiisoft/yii2-app-advanced, the API of RESTful-style web service service is realized. When the request parameter is multi-model, the realization of data filling and validation
1. Reference URL:https://www.yiiframework.com/doc/guide/2.0/zh-cn/input-multiple-models, it is suitable for web forms, but the request parameters of the API application generally do not add the form name (especially when the single model is entered), so it is not suitable, for example: the table name is article_type, the field name is code, then the request parameter name is article_type_code, not: articleType[‘code’], the web form is shown in Figure 1
2. The current number of models is 7, namely: QQ_TP_APP_PEGUIN (Penguin Media User of the third-party service platform application of Penguin, QQtpAppPPEGUIN), ARTICLE_TYPE (article type, Articl) eType), article_category (article classification, articleCategory), QQ_article_category_normal (article type (article) article classification, QQart ICleCategoryNormal), Article (Article, Article), QQ_Article_Normal (article article type (article) of Penguin, QQArticleNormal), QQ_T p_app_access_token (the access token (Redis) of the third-party service platform application of Penguin, RedisqqAuthqqtpappAccessToken), the example only demonstrates the filling and verification implementation of the first 2 models3. Types of published articles: Standard (common, graphic) articles are published on the channel /qq/v1/articles(article/create), the request parameter does not strictly follow the rules of (table name_field name) Then, because strict follow-up, the field name will be too long, and there may still be a problem that is not easy to distinguish, and it is necessary to manually convert it to the field of the corresponding model.
1、请求参数列表
(1)uuid:必填,企鹅号ID(UUID)
(2)article_type_code:必填,文章类型代码,standard:标准(普通)
(3)article_category_id:必填,文章分类ID
(4)title:必填,标题
(5)author:可选,作者,默认:空字符串
(6)source:必填,来源,xContent:内容库;vms:视频管理系统;cms:内容管理系统;spider:自媒体
(7)source_user_id:必填,来源用户ID
(8)source_article_id:必填,来源文章ID
(9)content:必填,文章内容
(10)cover_pic:必填,文章封面图
(11)cover_type:可选,文章封面类型,1:单图;3:三图,默认:1
(12)tag:可选,文章标签,以英文半角逗号分隔,默认:空字符串
(13)apply:可选,是否申请原创文章,0:否;1:是(需要用户具有发表图文原创文章资格否则无效),默认:0
(14)original_platform:可选,原创首发平台,申请原创文章时必填,默认:0
(15)original_url:可选,原创首发链接,申请原创文章时当选择平台不是企鹅号时必填,默认:空字符串
(16)original_author:可选,原创首发作者,申请原创文章时当选择平台不是企鹅号时必填,默认:空字符串
2、输入数据验证规则
(1)必填:uuid、article_type_code、article_category_id、title、source、source_user_id、source_article_id、content、cover_pic
(2)默认值(''):author、tag、original_url、original_author
(3)默认值(1):cover_type
(4)默认值(0):apply、original_platform
(5)比对:article_type_code 其值必须等于 standard
(6)存在性:uuid 必须存在于企鹅号的第三方服务平台应用的企鹅媒体用户模型中,且其状态为 1:启用
4. Create a new model file: \common\logics\qqArticleStandardCreateParam.php, its work When the first 4 and 6 items in the input data verification rule are used to ensure that all parameter values exist, it is convenient to convert to the corresponding model field, and there is no need to judge whether its value exists again.
''],
[['cover_type'], 'default', 'value' => 1],
[['apply', 'original_platform'], 'default', 'value' => 0],
['article_type_code', 'compare', 'compareValue' => ArticleType::CODE_STANDARD],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'uuid' => Yii::t('model/qq-article-standard-create-param', 'Uuid'),
'article_type_code' => Yii::t('model/qq-article-standard-create-param', 'Article Type Code'),
'article_category_id' => Yii::t('model/qq-article-standard-create-param', 'Article Category Id'),
'title' => Yii::t('model/qq-article-standard-create-param', 'Title'),
'author' => Yii::t('model/qq-article-standard-create-param', 'Author'),
'source' => Yii::t('model/qq-article-standard-create-param', 'Source'),
'source_user_id' => Yii::t('model/qq-article-standard-create-param', 'Source User Id'),
'source_article_id' => Yii::t('model/qq-article-standard-create-param', 'Source Article Id'),
'content' => Yii::t('model/qq-article-standard-create-param', 'Content'),
'cover_pic' => Yii::t('model/qq-article-standard-create-param', 'Cover Pic'),
'cover_type' => Yii::t('model/qq-article-standard-create-param', 'Cover Type'),
'tag' => Yii::t('model/qq-article-standard-create-param', 'Tag'),
'apply' => Yii::t('model/qq-article-standard-create-param', 'Apply'),
'original_platform' => Yii::t('model/qq-article-standard-create-param', 'Original Platform'),
'original_url' => Yii::t('model/qq-article-standard-create-param', 'Original Url'),
'original_author' => Yii::t('model/qq-article-standard-create-param', 'Original Author'),
];
}
}
5. The corresponding language package file
\common\messages\en-us\model\qq-article-standard-create-param.php
'Uuid',
'Article Type Code' => 'Article Type Code',
'Article Category Id' => 'Article Category Id',
'Title' => 'Title',
'Author' => 'Author',
'Source' => 'Source',
'Source User Id' => 'Source User Id',
'Source Article Id' => 'Source Article Id',
'Content' => 'Content',
'Cover Pic' => 'Cover Pic',
'Cover Type' => 'Cover Type',
'Tag' => 'Tag',
'Category' => 'Category',
'Apply' => 'Apply',
'Original Platform' => 'Original Platform',
'Original Url' => 'Original Url',
'Original Author' => 'Original Author',
];
\common\messages\zh-cn\model\qq-article-standard-create-param.php
'企鹅号ID(UUID)',
'Article Type Code' => '文章类型代码',
'Article Category Id' => '文章分类ID',
'Title' => '标题',
'Author' => '作者',
'Source' => '来源',
'Source User Id' => '来源用户ID',
'Source Article Id' => '来源文章ID',
'Content' => '文章内容',
'Cover Pic' => '文章封面图',
'Cover Type' => '文章封面类型,1:单图;3:三图',
'Tag' => '文章标签,以英文半角逗号分隔',
'Category' => '文章分类编号,即企鹅号的文章类型(文章)的文章分类ID',
'Apply' => '是否申请原创文章,0:否;1:是(需要用户具有发表图文原创文章资格否则无效)',
'Original Platform' => '原创首发平台,申请原创文章时必填',
'Original Url' => '原创首发链接,申请原创文章时当选择平台不是企鹅号时必填',
'Original Author' => '原创首发作者,申请原创文章时当选择平台不是企鹅号时必填',
];
6. In the model directory of the application, edit \qq\models\article.php to define the scenario
7. The model data layer of the public directory, QQ_TP_APP_PEGUIN (Penguin Media User, QQ qtpapppenguin), \common\models\qqtpapppenguin.php, the code is as follows
64],
[['openid'], 'string', 'max' => 32],
[['uuid'], 'unique'],
[['openid'], 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('model/qq-tp-app-penguin', 'ID'),
'uuid' => Yii::t('model/qq-tp-app-penguin', 'Uuid'),
'qq_tp_app_id' => Yii::t('model/qq-tp-app-penguin', 'Qq Tp App ID'),
'openid' => Yii::t('model/qq-tp-app-penguin', 'Openid'),
'status' => Yii::t('model/qq-tp-app-penguin', 'Status'),
'created_at' => Yii::t('model/qq-tp-app-penguin', 'Created At'),
'updated_at' => Yii::t('model/qq-tp-app-penguin', 'Updated At'),
];
}
/**
* {@inheritdoc}
* @return QqTpAppPenguinQuery the active query used by this AR class.
*/
public static function find()
{
return new QqTpAppPenguinQuery(get_called_class());
}
}
8. The model logic layer of the public directory, QQ_TP_APP_PEGUIN (Penguin Media User of the Third Party Service Platform Application of Penguin, Q qtpapppenguin), \common\logics\qqtpapppenguin.php, the code is as follows
[
'class' => TimestampBehavior::className(),
'attributes' => [
self::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
self::EVENT_BEFORE_UPDATE => 'updated_at',
SoftDeleteBehavior::EVENT_BEFORE_SOFT_DELETE => 'updated_at',
]
],
'uuid' => [
'class' => UUIDBehavior::className(),
'column' => 'uuid',
],
'softDeleteBehavior' => [
'class' => SoftDeleteBehavior::className(),
'softDeleteAttributeValues' => [
'status' => self::STATUS_DELETED
],
],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
$scenarios = parent::scenarios();
return $scenarios;
}
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
];
$parentRules = parent::rules();
return ArrayHelper::merge($rules, $parentRules);
}
/**
* {@inheritdoc}
* @return QqTpAppPenguinQuery the active query used by this AR class.
*/
public static function find()
{
return new QqTpAppPenguinQuery(get_called_class());
}
}
9. In the model directory of the application, QQ_TP_APP_PPEGUIN (Penguin Media User, QQTPA of the third-party service platform application of Penguin ppppenguin), \qq\models\qqtpapppenguin.php, define the verification rules for specific scenarios, the code is as follows
* @since 1.0
*/
class QqTpAppPenguin extends \common\logics\QqTpAppPenguin
{
/**
* {@inheritdoc}
*/
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[Article::SCENARIO_STANDARD_CREATE] = ['uuid'];
return $scenarios;
}
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
/* 发布文章类型:标准(普通、图文)的文章 */
[['uuid'], 'required', 'on' => Article::SCENARIO_STANDARD_CREATE],
['uuid', 'exist', 'filter' => ['status' => self::STATUS_ENABLED], 'on' => Article::SCENARIO_STANDARD_CREATE],
[['qq_tp_app_id', 'openid'], 'required'],
];
$parentRules = parent::rules();
unset($parentRules[0], $parentRules[4]);
return ArrayHelper::merge($rules, $parentRules);
}
/**
* {@inheritdoc}
* @return QqTpAppPenguinQuery the active query used by this AR class.
*/
public static function find()
{
return new QqTpAppPenguinQuery(get_called_class());
}
}
10. Editing method file \QQ\RESTS\Article\CreateAction.php
* @since 1.0
*/
class CreateAction extends Action
{
/**
* @var string the scenario to be assigned to the new model before it is validated and saved.
*/
public $scenario = Model::SCENARIO_DEFAULT;
/**
* @var string the name of the view action. This property is need to create the URL when the model is successfully created.
*/
public $viewAction = 'view';
/**
* Creates a new model.
* @return \yii\db\ActiveRecordInterface the model newly created
* @throws ServerErrorHttpException if there is any error when creating the model
*/
public function run()
{
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id);
}
$requestParams = Yii::$app->getRequest()->getBodyParams();
/* 判断请求体参数中租户ID是否存在 */
if (!isset($requestParams['group_id'])) {
$requestParams = ArrayHelper::merge($requestParams, ['group_id' => Yii::$app->params['groupId']]);
}
/* 标准(普通、图文)的文章发布参数 */
$qqArticleStandardCreateParam = new QqArticleStandardCreateParam();
// 把请求数据填充到模型中
if (!$qqArticleStandardCreateParam->load($requestParams, '')) {
return ['code' => 40009, 'message' => Yii::t('error', '40009')];
}
// 验证模型
if (!$qqArticleStandardCreateParam->validate()) {
$qqArticleStandardCreateParamResult = self::handleValidateError($qqArticleStandardCreateParam);
if ($qqArticleStandardCreateParamResult['status'] === false) {
return ['code' => $qqArticleStandardCreateParamResult['code'], 'message' => $qqArticleStandardCreateParamResult['message']];
}
}
/* 基于文章类型代码定义场景 */
$this->scenario = 'qq_article_' . $qqArticleStandardCreateParam->article_type_code . '_create';
/* 实例化多个模型 */
// 企鹅号的第三方服务平台应用的企鹅媒体用户
$qqTpAppPenguin = new QqTpAppPenguin([
'scenario' => $this->scenario,
]);
// 转换标准(普通、图文)的文章发布参数,多模型的填充、验证的实现
$requestParams[$qqTpAppPenguin->formName()]['uuid'] = $qqArticleStandardCreateParam->uuid;
$qqTpAppPenguinResult = self::handleLoadAndValidate($qqTpAppPenguin, $requestParams);
if ($qqTpAppPenguinResult['status'] === false) {
return ['code' => $qqTpAppPenguinResult['code'], 'message' => $qqTpAppPenguinResult['message']];
}
return ['code' => 10000, 'message' => Yii::t('app', '10003'), 'data' => ''];
}
/**
* 处理模型填充与验证
* @param object $model 模型
* @param array $requestParams 请求参数
* @return array
* 格式如下:
*
* [
* 'status' => true, // 成功
* ]
*
* [
* 'status' => false, // 失败
* 'code' => 20004, // 返回码
* 'message' => '数据验证失败:企鹅号ID(UUID)是无效的。', // 说明
* ]
*
* @throws ServerErrorHttpException
*/
public static function handleLoadAndValidate($model, $requestParams)
{
// 把请求数据填充到模型中
if (!$model->load($requestParams)) {
return ['status' => false, 'code' => 40009, 'message' => Yii::t('error', '40009')];
}
// 验证模型
if (!$model->validate()) {
return self::handleValidateError($model);
}
return ['status' => true];
}
/**
* 处理模型错误
* @param object $model 模型
* @return array
* 格式如下:
*
* [
* 'status' => false, // 失败
* 'code' => 20004, // 返回码
* 'message' => '数据验证失败:代码是无效的。', // 说明
* ]
*
* @throws ServerErrorHttpException
*/
public static function handleValidateError($model)
{
if ($model->hasErrors()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(422, 'Data Validation Failed.');
foreach ($model->getFirstErrors() as $message) {
$firstErrors = $message;
break;
}
return ['status' => false, 'code' => 20004, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '20004'), ['firstErrors' => $firstErrors]))];
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
}
}
}
11. Posthttp://api.channel-pub-api.localhost/qq/v1/articles?group_id=015ce30b116ce86058fa6ab4fea4ac63, Response: Data validation failed: Penguin ID (UUID) cannot be empty. , in line with expectations
{
}
{
"code": 20004,
"message": "数据验证失败:企鹅号ID(UUID)不能为空。"
}
12. Posthttp://api.channel-pub-api.localhost/qq/v1/articles?group_id=015ce30b116ce86058fa6ab4fea4ac63, when the parameter is missing: original_author, print: $qqArticleStandardCreateParam, the value of original_author is an empty string, which is in line with expected
{
"uuid": "e88e79faad9011e8a14554ee75d2ebc1",
"article_type_code": "standard",
"article_category_id": 1,
"title": "标题 - 20180901 - 1",
"author": "作者 - 20180901 - 1",
"source": "spider",
"source_user_id": 1,
"source_article_id": 1,
"content": "文章内容 - 20180901 - 1",
"cover_pic": "http://b.hiphotos.baidu.com/image/pic/item/1e30e924b899a901a5be490c10950a7b0208f505.jpg",
"cover_type": 1,
"tag": "",
"apply": 0,
"original_platform": 0,
"original_url": ""
}
qq\models\QqArticleStandardCreateParam Object
(
[uuid] => e88e79faad9011e8a14554ee75d2ebc1
[article_type_code] => standard
[article_category_id] => 1
[title] => 标题 - 20180901 - 1
[author] => 作者 - 20180901 - 1
[source] => spider
[source_user_id] => 1
[source_article_id] => 1
[content] => 文章内容 - 20180901 - 1
[cover_pic] => http://b.hiphotos.baidu.com/image/pic/item/1e30e924b899a901a5be490c10950a7b0208f505.jpg
[cover_type] => 1
[tag] =>
[apply] => 0
[original_platform] => 0
[original_url] =>
[original_author] =>
[_errors:yii\base\Model:private] => Array
(
)
)
13. Posthttp://api.channel-pub-api.localhost/qq/v1/articles?group_id=015ce30b116ce86058fa6ab4fea4ac63, parameter: UUID does not exist in the Penguin Media user model of the third-party service platform application of Penguin, and the response: data verification failed: The Penguin ID (UUID) is invalid. , as expected, as shown in Figure 2
{
"uuid": "e88e79faad9011e8a14554ee75d2ebc10",
"article_type_code": "standard",
"article_category_id": 1,
"title": "标题 - 20180901 - 1",
"author": "作者 - 20180901 - 1",
"source": "spider",
"source_user_id": 1,
"source_article_id": 1,
"content": "文章内容 - 20180901 - 1",
"cover_pic": "http://b.hiphotos.baidu.com/image/pic/item/1e30e924b899a901a5be490c10950a7b0208f505.jpg",
"cover_type": 1,
"tag": "",
"apply": 0,
"original_platform": 0,
"original_url": "",
"original_author": ""
}
{
"code": 20004,
"message": "数据验证失败:企鹅号ID(UUID)是无效的。"
}
14. View the log, the SQL statement in the verification process
SELECT EXISTS(SELECT * FROM `cpa_qq_tp_app_penguin` WHERE (`cpa_qq_tp_app_penguin`.`uuid`='e88e79faad9011e8a14554ee75d2ebc10') AND (`status`=1))
15. The verification scheme of other models in subsequent models can follow the verification scheme of UUID, and the final code can be realized when the request parameter is multi-model, and the data is filled and validated.
* @since 1.0
*/
class CreateAction extends Action
{
/**
* @var string the scenario to be assigned to the new model before it is validated and saved.
*/
public $scenario = Model::SCENARIO_DEFAULT;
/**
* @var string the name of the view action. This property is need to create the URL when the model is successfully created.
*/
public $viewAction = 'view';
/**
* Creates a new model.
* @return \yii\db\ActiveRecordInterface the model newly created
* @throws ServerErrorHttpException if there is any error when creating the model
*/
public function run()
{
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id);
}
$requestParams = Yii::$app->getRequest()->getBodyParams();
/* 判断请求体参数中租户ID是否存在 */
if (!isset($requestParams['group_id'])) {
$requestParams = ArrayHelper::merge($requestParams, ['group_id' => Yii::$app->params['groupId']]);
}
/* 标准(普通、图文)的文章发布参数 */
$qqArticleStandardCreateParam = new QqArticleStandardCreateParam();
// 把请求数据填充到模型中
if (!$qqArticleStandardCreateParam->load($requestParams, '')) {
return ['code' => 40009, 'message' => Yii::t('error', '40009')];
}
// 验证模型
if (!$qqArticleStandardCreateParam->validate()) {
$qqArticleStandardCreateParamResult = self::handleValidateError($qqArticleStandardCreateParam);
if ($qqArticleStandardCreateParamResult['status'] === false) {
return ['code' => $qqArticleStandardCreateParamResult['code'], 'message' => $qqArticleStandardCreateParamResult['message']];
}
}
/* 基于文章类型代码定义场景 */
$this->scenario = 'qq_article_' . $qqArticleStandardCreateParam->article_type_code . '_create';
/* 实例化多个模型 */
// 企鹅号的第三方服务平台应用的企鹅媒体用户
$qqTpAppPenguin = new QqTpAppPenguin([
'scenario' => $this->scenario,
]);
// 转换标准(普通、图文)的文章发布参数,多模型的填充、验证的实现
$requestParams[$qqTpAppPenguin->formName()]['uuid'] = $qqArticleStandardCreateParam->uuid;
$qqTpAppPenguinResult = self::handleLoadAndValidate($qqTpAppPenguin, $requestParams);
if ($qqTpAppPenguinResult['status'] === false) {
return ['code' => $qqTpAppPenguinResult['code'], 'message' => $qqTpAppPenguinResult['message']];
}
// 文章分类
$articleCategory = new ArticleCategory([
'scenario' => $this->scenario,
]);
// 转换标准(普通、图文)的文章发布参数,多模型的填充、验证的实现
$requestParams[$articleCategory->formName()]['id'] = $qqArticleStandardCreateParam->article_category_id;
$articleCategoryResult = self::handleLoadAndValidate($articleCategory, $requestParams);
if ($articleCategoryResult['status'] === false) {
return ['code' => $articleCategoryResult['code'], 'message' => $articleCategoryResult['message']];
}
// 如果当前场景为:qq_article_standard_create
if ($this->scenario == Article::SCENARIO_STANDARD_CREATE) {
// 文章分类
$qqArticleCategoryNormal = new QqArticleCategoryNormal([
'scenario' => $this->scenario,
]);
// 转换标准(普通、图文)的文章发布参数,多模型的填充、验证的实现
$requestParams[$qqArticleCategoryNormal->formName()]['article_category_id'] = $qqArticleStandardCreateParam->article_category_id;
$qqArticleCategoryNormalResult = self::handleLoadAndValidate($qqArticleCategoryNormal, $requestParams);
if ($qqArticleCategoryNormalResult['status'] === false) {
return ['code' => $qqArticleCategoryNormalResult['code'], 'message' => $qqArticleCategoryNormalResult['message']];
}
}
// 文章
$model = new $this->modelClass([
'scenario' => $this->scenario,
]);
// 转换标准(普通、图文)的文章发布参数,多模型的填充、验证的实现
$requestParams[$model->formName()] = [
'group_id' => $requestParams['group_id'],
'title' => $qqArticleStandardCreateParam->title,
'author' => $qqArticleStandardCreateParam->author,
'source' => $qqArticleStandardCreateParam->source,
'source_user_id' => $qqArticleStandardCreateParam->source_user_id,
'source_article_id' => $qqArticleStandardCreateParam->source_article_id,
];
$modelResult = self::handleLoadAndValidate($model, $requestParams);
if ($modelResult['status'] === false) {
return ['code' => $modelResult['code'], 'message' => $modelResult['message']];
}
// 如果当前场景为:qq_article_standard_create
if ($this->scenario == Article::SCENARIO_STANDARD_CREATE) {
// 企鹅号的文章类型(文章)的文章
$qqArticleNormal = new QqArticleNormal([
'scenario' => $this->scenario,
]);
// 转换标准(普通、图文)的文章发布参数,多模型的填充、验证的实现
$requestParams[$qqArticleNormal->formName()] = [
'content' => $qqArticleStandardCreateParam->content,
'cover_pic' => $qqArticleStandardCreateParam->cover_pic,
'cover_type' => $qqArticleStandardCreateParam->cover_type,
'tag' => $qqArticleStandardCreateParam->tag,
'apply' => $qqArticleStandardCreateParam->apply,
'original_platform' => $qqArticleStandardCreateParam->original_platform,
'original_url' => $qqArticleStandardCreateParam->original_url,
'original_author' => $qqArticleStandardCreateParam->original_author,
];
$qqArticleNormalResult = self::handleLoadAndValidate($qqArticleNormal, $requestParams);
if ($qqArticleNormalResult['status'] === false) {
return ['code' => $qqArticleNormalResult['code'], 'message' => $qqArticleNormalResult['message']];
}
}
// 企鹅号的第三方服务平台应用的访问令牌(Redis)
$redisQqAuthQqTpAppAccessToken = new RedisQqAuthQqTpAppAccessToken([
'scenario' => $this->scenario,
]);
// 转换标准(普通、图文)的文章发布参数,多模型的填充、验证的实现
$requestParams[$redisQqAuthQqTpAppAccessToken->formName()]['qq_tp_app_penguin_uuid'] = $qqArticleStandardCreateParam->uuid;
$redisQqAuthQqTpAppAccessTokenResult = self::handleLoadAndValidate($redisQqAuthQqTpAppAccessToken, $requestParams);
if ($redisQqAuthQqTpAppAccessTokenResult['status'] === false) {
throw new HttpException(302, Yii::t('error', '40008'), 40008);
}
return ['code' => 10000, 'message' => Yii::t('app', '10003'), 'data' => ''];
}
/**
* 处理模型填充与验证
* @param object $model 模型
* @param array $requestParams 请求参数
* @return array
* 格式如下:
*
* [
* 'status' => true, // 成功
* ]
*
* [
* 'status' => false, // 失败
* 'code' => 20004, // 返回码
* 'message' => '数据验证失败:企鹅号ID(UUID)是无效的。', // 说明
* ]
*
* @throws ServerErrorHttpException
*/
public static function handleLoadAndValidate($model, $requestParams)
{
// 把请求数据填充到模型中
if (!$model->load($requestParams)) {
return ['status' => false, 'code' => 40009, 'message' => Yii::t('error', '40009')];
}
// 验证模型
if (!$model->validate()) {
return self::handleValidateError($model);
}
return ['status' => true];
}
/**
* 处理模型错误
* @param object $model 模型
* @return array
* 格式如下:
*
* [
* 'status' => false, // 失败
* 'code' => 20004, // 返回码
* 'message' => '数据验证失败:代码是无效的。', // 说明
* ]
*
* @throws ServerErrorHttpException
*/
public static function handleValidateError($model)
{
if ($model->hasErrors()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(422, 'Data Validation Failed.');
foreach ($model->getFirstErrors() as $message) {
$firstErrors = $message;
break;
}
return ['status' => false, 'code' => 20004, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '20004'), ['firstErrors' => $firstErrors]))];
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
}
}
}
![参考网址:https://www.yiiframework.com/doc/guide/2.0/zh-cn/input-multiple-models ,其用于网页表单是合适的,不过 API 应用的请求参数一般并未添加表单名称(尤其是单个模型输入时),因此,不太合适,例:表名为 article_type,字段名为 code,那么请求参数名为 article_type_code,而不是:ArticleType['code'],网页表单](https://www.shuijingwanwq.com/wp-content/uploads/2018/09/1.png)
