yii2 loadMultiple validateMultiple
示例:
PHP Code复制内容到剪贴板
- $multiple = [];
- // 需要组合成:
- // {
- // "Config": [
- // {
- // "name": "backend_login_name",
- // "value": "yoyocmf 后台管理系统"
- // },
- // {
- // "name": "backend_login_fname",
- // "value": "yoyocmf Background management system"
- // },
- // {
- // "name": "backend_name",
- // "value": "yoyocmf"
- // },
- // ]
- // }
- $names = ["backend_login_name","backend_login_fname","backend_name"];
- $configModels = Config::find()->where(['in','name',$names])->all();
- ## 要求,对这几个name的value update
- if (Model::loadMultiple($configModels, $multiple) && Model::validateMultiple($configModels)) {
- foreach ($configModels as $configModel) {
- /* @var $config Config */
- $configModel->save(false);
- }
- TagDependency::invalidate(\Yii::$app->cache, Yii::$app->config->cacheTag);
- Yii::$app->session->setFlash('success', '保存成功');
- return $this->goBack();
- } else {
- Yii::$app->session->setFlash('error', '保存失败');
- return $this->redirect(['index', 'group' => $group]);
- }
validateMultiple 只返回bool值,无法看到错误提示,如何调试
PHP Code复制内容到剪贴板
- $e = Model::loadMultiple($configModels, $multiple);
- echo "结果:" . $e;
- foreach ($configModels as $k => $model){
- if(!$model->validate()){
- p($model->errors);
- };
- }
- if(Model::validateMultiple($configModels)){
- p("成功");
- }else{
- p($configModels->errors());
- p("失败");
- }
上一篇 yii2 migrate
下一篇 yii2-apidoc