Answer the question
In order to leave comments, you need to log in
How to save id to model?
There are two related models - only one has an id, so the validation rules are empty
public function rules()
{
return
[];
}
form->field($model, 'id')->textInput()
<?php // $form->field($model, 'id')->textInput() ?>
<?= $form->field($modelLang, 'direction_name')->textInput() ?>
$model = new Direction();
$modelLang = new DirectionLang();
if ($model->load(Yii::$app->request->post()) &&
$modelLang->load(Yii::$app->request->post()) && $model->save()) {
$modelLang->lang_id = 2;
$modelLang->direction_id = $model->id;
$modelLang->save();
var_dump($model->getErrors());
var_dump($modelLang->getErrors());
return $this->redirect(['view', 'id' => $model->id]);
}
'id' => $this->primaryKey(),
<?php echo $form->field($model, 'id')->hiddenInput()->label(false); ?>
but there is probably some other way
Answer the question
In order to leave comments, you need to log in
Good morning.
Naturally, you will not save because you do not load the model from POST, it simply is not there. For this, you needed a hidden input so that you could load the required model via $model->load()
. Therefore, with a hidden input, everything works.
If you remove the loading of a model in which there is only one id from the code, then everything will work.
For example, a model with a single attribute id you have Direction. So in action you need to do this
$model = new Direction();
$modelLang = new DirectionLang();
if ($modelLang->load(Yii::$app->request->post()) && $model->save()) {
$modelLang->lang_id = 2;
$modelLang->direction_id = $model->id;
$modelLang->save();
var_dump($model->getErrors());
var_dump($modelLang->getErrors());
return $this->redirect(['view', 'id' => $model->id]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question