示例#1
0
 /**
  * Updates an existing Language model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $username = Yii::$app->user->identity->username;
     if ($model = $this->findModel($id)) {
         $this->language_id = $model->id;
         if (Yii::$app->request->isPost && $model->update2(Yii::$app->request->post())) {
             foreach (LanguageData::findAll(['language_id' => $model->id]) as $item) {
                 if (!in_array($item->key, I18n::$language_keys)) {
                     $item->delete();
                 }
             }
             $default = Language::getDefault();
             foreach (I18n::$language_keys as $key) {
                 if (!LanguageData::findOne(['language_id' => $model->id, 'key' => $key])) {
                     if ($default_item = LanguageData::findOne(['language_id' => $default->id, 'key' => $key])) {
                         $value = $default_item->value;
                     } else {
                         $value = $key;
                     }
                     LanguageData::create(['LanguageData' => ['language_id' => $model->id, 'key' => $key, 'value' => $value]]);
                 }
             }
             return $this->goBack(Url::previous());
         } else {
             return $this->render('update', ['username' => $username, 'model' => $model]);
         }
     } else {
         throw new NotFoundHttpException();
     }
 }
 /**
  * Finds the LanguageData model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return LanguageData the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = LanguageData::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }