コード例 #1
0
ファイル: ConfigAction.php プロジェクト: skinka/yii2-config
 /**
  * @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]);
 }
コード例 #2
0
ファイル: Config.php プロジェクト: skinka/yii2-config
 /**
  * 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];
 }