/** * @return array|string|Response */ public function run() { $models = ConfigModel::find()->orderBy(['sort' => SORT_ASC])->indexBy('name')->all(); if (Model::loadMultiple($models, Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validateMultiple($models, 'value'); } $result = true; /** @var ConfigModel $model */ foreach ($models as $model) { $result = $result && $model->save(true, ['value']); Config::clearCache($model->name); } if ($result) { Yii::$app->session->setFlash('success', $this->successMessage); return $this->controller->redirect([$this->id]); } } return $this->controller->render($this->viewPath, ['models' => $models]); }
/** * Get instance ConfigModel by name setting * @param string $name * @return ConfigModel */ private static function getInstance($name) { if (!isset(static::$_instance[$name])) { $data = ConfigModel::find()->where(['name' => $name])->one(); if (!$data) { throw new \BadMethodCallException(); } static::$_instance[$name] = $data; } return static::$_instance[$name]; }