/** * 更新配置缓存 * @return boolean */ public function updateCache() { $cache = Yii::$app->getCache(); $models = Setting::find()->all(); //整个缓存表,包括不可见内容 //处理数组形式的数据 foreach ($models as $key => $model) { if ($model->is_array) { $values = explode(',', $model->value); $newValues = []; if ($values) { foreach ($values as $value) { $v = explode('=>', $value); $newValues[$v[0]] = $v[1]; } } $models[$key]->value = $newValues; } } $data = Json::encode(ArrayHelper::map($models, 'key', 'value')); //获取数据 if ($cache->exists(self::CACHE_KEY)) { $cache->delete(self::CACHE_KEY); } $cache->set(self::CACHE_KEY, $data); return true; }
public function actionConfig() { if (Yii::$app->request->post()) { $model = new Setting(); if ($model->batchSave(Yii::$app->request->post()[$model->formName()]) && $model->updateCache()) { Yii::$app->getSession()->setFlash('success', Yii::t('system', 'Save Config Success!')); return $this->redirect(['config']); } } else { $models = Setting::find()->where(['is_visible' => Setting::SETTING_ACTIVE])->all(); $newModels = []; foreach ($models as $model) { $newModels[$model->key] = $model; } return $this->render('config', ['models' => $newModels]); } }