mysql - Yii2: How to save form data in two tables from one form? -


public function actioncreate()     {         $model = new createclient1();         $employee = new employee();           if ($model->load(yii::$app->request->post()) && $employee->load(yii::$app->request->post()))          {             $model->save();              /*add same field in employee table*/              $employee->client_code = $model->client_code;             $employee->company_name = $model->company_name;             $employee->emp_first_name = $model->emp_first_name;             $employee->emp_last_name = $model->emp_last_name;             $employee->emp_email = $model->emp_email;             $employee->emp_mobile = $model->emp_mobile;             $employee->save();             return $this->redirect(['view', 'id' => $model->id]);         } else          {             return $this->render('create', [                 'model' => $model,                 'employee' => $employee,             ]);         }     } 

my form looks need add in it?

<?php  use yii\helpers\html; use yii\widgets\activeform; use wbraganca\dynamicform\dynamicformwidget;  /* @var $this yii\web\view */ /* @var $model backend\models\createclient1 */ /* @var $form yii\widgets\activeform */ ?>  <div class="create-client1-form">     <?php $form = activeform::begin(['id' => 'dynamic-form']); ?>      <?= html::activehiddeninput($model, 'client_code', ['value' => rand(1,100000000000000)]) ?>      <?= $form->field($model, 'company_name')->textinput(['maxlength' => true]) ?>      <?= $form->field($model, 'emp_email')->textinput(['maxlength' => true]) ?>      <?= $form->field($model, 'emp_mobile')->textinput(['maxlength' => true]) ?>      <?= $form->field($model, 'emp_first_name')->textinput(['maxlength' => true]) ?>      <?= $form->field($model, 'emp_last_name')->textinput(['maxlength' => true]) ?>     </div>      <div class="form-group">         <?= html::submitbutton($model->isnewrecord ? 'create' : 'update', ['class' => $model->isnewrecord ? 'btn btn-success' : 'btn btn-primary']) ?>     </div>      <?php activeform::end(); ?>  </div> 

trying insert same data in 2 tables. table createclient1 , employee has same data, how insert in employee table in yii2. thing needs add in form? form form not submitting

try this:

public function actioncreate()     {         $model = new createclient1();         $employee = new employee();           if ($model->load(yii::$app->request->post()))          {             $model->save();              /*add same field in employee table*/             $employee->attributes = $model->attributes;             $employee->save();              return $this->redirect(['view', 'id' => $model->id]);         } else          {             return $this->render('create', [                 'model' => $model,                 'employee' => $employee,             ]);         }     } 

Comments