简体   繁体   中英

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 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 to insert same data in two tables. Table CreateClient1 and Employee has to be same data, how do I insert in to Employee table in Yii2. Any thing needs to add in form? my form form is 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,
            ]);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM