public function actionIndexWithDescriptions() { if (isset($_POST['Room'])) { $roomsInput = $_POST['Room']; foreach ($roomsInput as $item) { $sourceMessage = \common\models\SourceMessage::findOne(['message' => $item['description']]); // If null, I need to create source message if ($sourceMessage == null) { $sourceMessage = new \common\models\SourceMessage(); } $sourceMessage->category = 'app'; $sourceMessage->message = $item['description']['en']; $sourceMessage->save(); $otherLanguages = ['zh-cn']; foreach ($otherLanguages as $otherLang) { $message = \common\models\Message::findOne(['id' => $sourceMessage->id, 'language' => $otherLang]); if ($message == null) { $message = new \common\models\Message(); } $message->id = $sourceMessage->id; $message->language = $otherLang; $message->translation = $item['description'][$otherLang]; $message->save(); } // Room to update $roomToUpdate = \frontend\models\BaseRoom::findOne($item['id']); $roomToUpdate->description = $item['description']['en']; $roomToUpdate->save(); } } $rooms = BaseRoom::find()->all(); return $this->render('indexWithDescriptions', ['rooms' => $rooms]); }
/** * Finds SourceMessage model by quering or creating new one * @param string $field Name of the owner's field that's linked * @param bool $onlyMessage Whether to return model instance or string * @param bool $translation Whether to return translated message. Be sure * that $language param is set and it differs * from $defaultLanguage * @return mixed \common\models\SourceMessage or string * depending on the $onlyMessage param */ public function findSourceMessage($field, $onlyMessage = true, $translation = true) { if (!empty($this->_messages[$field])) { $model = $this->_messages[$field]; return $onlyMessage ? $model->message : $model; } $owner = $this->owner; $model = null; if (!empty($owner->{$field})) { $model = SourceMessage::findOne($owner->{$field}); } if ($model === null) { $model = new SourceMessage(['category' => $owner::tableName(), 'message' => null]); } $this->_messages[$field] = $model; $language = $this->language; $defaultLanguage = $this->defaultLanguage; // Get translated model if $model->language is set if (!empty($language && $language !== $defaultLanguage) && $translation) { return $model->getTranslation($language, $onlyMessage); } return $onlyMessage === true ? $model->message : $model; }
/** * Finds the SourceMessage model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return SourceMessage the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = SourceMessage::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }