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,
]);
}
}
我的表单看起来像我需要在其中添加内容吗?
<?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>
尝试在两个表中插入相同的数据。 表CreateClient1和Employee必须是相同的数据,如何插入Yii2中的Employee表。 任何东西需要添加形式?我的表单没有提交
答案 0 :(得分:2)
试试这个:
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,
]);
}
}