I want to keep bilingual content. The form:

<?php $form = ActiveForm::begin(); ?> <?= $form->field($aboutLang_ru, 'title')->textInput() ?> <?= $form->field($aboutLang_ru, 'content')->textInput() ?> <?= $form->field($aboutLang_en, 'title')->textInput() ?> <?= $form->field($aboutLang_en, 'content')->textInput() ?> <div class="form-group"> <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> 

And controller

 public function actionCreate() { $model = new Post(); $aboutLang_ru = new PostLang(); $aboutLang_en = new PostLang(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { $dbPost = new PostLang(); $dbPost->title = $aboutLang_ru->title; $dbPost->content = $aboutLang_ru->content; $dbPost->lang_id = ////////; $dbPost->post_id = $model->id; $dbPost->save(); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('_form', [ 'model' => $model, 'aboutLang_ru' => $aboutLang_ru, 'aboutLang_en' => $aboutLang_en]); } } 

Here's how I do a brute force saving to save first from $ aboutLang_ru, and then from $ aboutLang_en? Different entries in the table and with different lang_id, but with the same post_id?

    3 answers 3

    Sorry, I probably did not understand your question. And what's stopping you from saving two different Postlang model instances just sequentially? By specifying each with the appropriate field values? if ($ model-> save ()) {

      $dbPost = new PostLang(); $dbPost->title = $aboutLang_ru->title; $dbPost->content = $aboutLang_ru->content; $dbPost->lang_id = ////////; $dbPost->post_id = $model->id; $dbPost->save(); $dbPost2 = new PostLang(); $dbPost2->title = $aboutLang_en->title; $dbPost2->content = $aboutLang_en->content; $dbPost2->lang_id = ////////; $dbPost2->post_id = $model->id; $dbPost2->save(); } 

      And you can even easier than the option described above:

       if($model->save()) { $dbPost = new PostLang(); $dbPost->title = $aboutLang_ru->title; $dbPost->content = $aboutLang_ru->content; $dbPost->lang_id = 1; $dbPost->post_id = $model->id; $dbPost->save(); //сбрасывает состояние записи, к новой, теперь вместо перезаписи, //сохранится новая запись $dbPost->isNewRecord = 1; $dbPost->id=null //меняем только те значения которые нужно $dbPost->title = $aboutLang_en->title; $dbPost->content = $aboutLang_en->content; $dbPost->lang_id = 2; //сохраняем $dbPost->save(); } 
      • The primary key must also be deleted. But for aesthetics, I would clone the second instance, and then what you suggested. - dlarchikov
      • Yes, I forgot about the elephant, corrected it. - Dmitry Plavinsky

      If you are still fundamentally brute force

       if ($model->save()) { $langs = [$aboutLang_ru, $aboutLang_en]; foreach($langs as $lang){ $dbPost = new PostLang(); $dbPost->title = $lang->title; $dbPost->content = $lang->content; $dbPost->lang_id = $lang->id; $dbPost->post_id = $model->id; $dbPost->save(); } 

      }