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 like this: do I need to add something to 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 to insert the same data into two tables. CreateClient1 and Employee table should be the same data, how do I insert into Employee table in Yii2. Anything you need to add to the form? my form form is not submitting

+3


source to share


1 answer


Try the following:



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

      

+2


source







All Articles