Answer the question
In order to leave comments, you need to log in
How to save the model by iterating?
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(); ?>
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]);
}
}
Answer the question
In order to leave comments, you need to log in
$langs = ['aboutLang_ru','aboutLang_en'];
foreach($langs as $lang){
// тут код
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question