S
S
slip312019-05-31 23:42:20
Yii
slip31, 2019-05-31 23:42:20

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
         [];
    }

The second contains a name that I associate with the id of the previous model.
If you create a CRUD of the first model, then it generates a form with an ID insert. At the same time, when you click on the "save" button, everything is OK - it is generated and written to the id database. But, of course, I don't want to bring it in. However, if I remove this field, then the model does not save the ID, although it should (well, it seems to me) to do this automatically. All var_dump($model->gerErrors()) output nothing. Form with id commented out:
form->field($model, 'id')->textInput()

<?php // $form->field($model, 'id')->textInput() ?>    
    <?= $form->field($modelLang, 'direction_name')->textInput() ?>

Controller
$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]);
        }

How to get the id of the Direction model without entering it in the field?
He still 'id' => $this->primaryKey(),
PS So far decided so
<?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

2 answer(s)
D
Dmitry, 2019-06-01
@slip31

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]);
}

In this case, everything will work and you can remove the hidden input from the form.

G
grinat, 2019-06-01
@grinat

There is no other way to find out which model to save, you need to know its id. Or push the vanga into the server, and it will determine which record the person deigned to edit)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question