Task: When adding a record, enter data in several tables. Information from user data is entered into one table, basically static information into the other, except for one field. In one of the models declared variable

public $text; 

In the view added

 <?= $form->field($model, 'text')->textarea(['rows' => '6']); ?> 

It turns out that all the data from this model is transmitted, and nothing is transferred to the added field. How to make the data in the new field?

Controller

 public function actionCreate() { $model = new Helpdesk(); $model->status = 1; $model->priority = 1; $model->user_id = Yii::$app->user->identity['id']; $model->addtime = date('Ymd H:i:s'); exit($model->text); if ($model->load(Yii::$app->request->post()) && $model->save()) { $answers = new HelpdeskAnswers(); $answers->user_id = Yii::$app->user->identity['id']; $answers->user_id_agent = 0; $answers->date = date('Ymd H:i:s'); $answers->text = $model->text; $answers->helpdesk_id = $model->id; $answers->save(); return $this->redirect(['my']); } else { return $this->render('create', [ 'model' => $model, ]); } } 

PS I do not know how to make a title

    1 answer 1

    After you save the model, you have an instance of the class in the controller.

    do it this way (save the main model after the additional one):

     public function actionCreate() { ... if ($model->load(Yii::$app->request->post())) { //сохранение было здесь ... $answers->text = $model->text; $answers->helpdesk_id = $model->id; $answers->save(); //сохранение основной модели после сохранения другой $model->save(); return $this->redirect(['my']); } return $this->render('create', [ 'model' => $model, ]); } 

    or so (write the values ​​of the main model into variables and save it):

     public function actionCreate() { ... $id = $model->id; $text = $model->text; if ($model->load(Yii::$app->request->post()) && $model->save()) { //здесь оно так и осталось ... $answers->text = $text; $answers->helpdesk_id = $id; $answers->save(); return $this->redirect(['my']); } return $this->render('create', [ 'model' => $model, ]); } 
    • Forgot to mention. Added an entry in the rules - AntaGonist
    • @AntaGonist, you need to transfer from model to form, I understand correctly? How to update? - dasauser
    • No, when adding a new record, in addition to being added to one table, more information was added to another - AntaGonist
    • @AntaGonist, add your controller action to the question. - dasauser
    • added to the question - AntaGonist