php - how to insert data to 2 tables i.e Employee and User(migrated) from single form(Employee Create) and controller in yii2 -
this create page questions/create.php
`
<?= $form->field($model, 'clinic_id')->dropdownlist( arrayhelper::map(clinic::find()->all(),'id','clinic_name'), ['prompt'=> 'select clinic'] )?> <?= $form->field($model, 'first_name')->textinput(['maxlength' => true]) ?> <?= $form->field($model, 'last_name')->textinput(['maxlength' => true]) ?> <?= $form->field($model, 'user_name')->textinput(['maxlength' => true]) ?> <?= $form->field($model, 'password')->passwordinput(['maxlength' => true]) ?> <?= $form->field($model, 'email')->textinput(['maxlength' => true]) ?> <?= $form->field($model, 'mobile')->textinput(['maxlength' => true]) ?> <?= $form->field($model, 'is_active')->textinput() ?> <div class="form-group"> <?= html::submitbutton($model->isnewrecord ? 'create' : 'update', ['class' => $model->isnewrecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php activeform::end(); ?>
`
employee controller
public function actioncreate() { $model = new employee(); if ($model->load(yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } }
i have migrated user table , included class in controller data not inserting in user table (only in employee table) possible way.
acutely in advanced yii2 users can login user table want data employee table send user table also.
if there other better way employee can created , logged in
add following code to create action
public function actioncreate() { $employee = new employee(); $user = new user(); if($user->load(yii::$app->request->post()) , $employee->load(yii::$app->request->post()) , model::validatemultiple([$user, $employee])) { if($user->save(false) , $employee->save(false)) { return $this->redirect(['index']); } else{ return $this->render('create', [ 'employee' => $employee, 'user' => $user, ]); } } else { return $this->render('create', [ 'employee' => $employee, 'user' => $user, ]); } }
now in form write attribute , model related,like suppose first_name , last_name employee model , password ans email user model then
<?= $form->field($employee, 'first_name')->textinput(['maxlength' => true]) ?> <?= $form->field($employee, 'last_name')->textinput(['maxlength' => true]) ?> <?= $form->field($user, 'user_name')->textinput(['maxlength' => true]) ?> <?= $form->field($user, 'password')->passwordinput(['maxlength' => true]) ?> <?= $form->field($user, 'email')->textinput(['maxlength' => true]) ?>
Comments
Post a Comment