0

I am working on my collage project i.e. Employee Management. I have Employee table in sql(crud is also generated from gii). only Admin is having rights to create Employee (there is no Signup).

My Problem: when I am creating employee then I am not able to save data in user table also, please help me to save data in both Employee and user table.

Thanks in advance

Update:

Below is the code:

public function actionCreate() { 
 $model1=new Employee; 
 $model2=new User; 
 if(isset($_POST['Employee']) && isset($_POST['User']))
 { 
   $model1->attributes=$_POST['Emoloyee']; 
   $model2->attributes=$_POST['User']; 
   $model1->save(); 
   $model2->save(); 
   echo 'data is saved in both tables'; 
 } 
 $this->render('create',array('model1'=>$model1,model2'=>$mod‌​‌​el2)); 
}
Chinmay Waghmare
  • 5,368
  • 2
  • 43
  • 68
  • 1
    What you have tried so far? – Insane Skull Sep 12 '17 at 08:48
  • Can you show the code for saving employee? – Chinmay Waghmare Sep 12 '17 at 08:49
  • public function actionCreate() { $model1=new Employee; $model2=new User; if(isset($_POST['Employee']) && isset($_POST['User'])) { $model1->attributes=$_POST['Emoloyee']; $model2->attributes=$_POST['User']; $model1->save(); $model2->save(); echo 'data is saved in both tables'; } $this->render('create',array('model1'=>$model1,model2'=>$model2)); } – Yuvraj verma Sep 12 '17 at 08:53
  • hi Chinmay code for my Employee Controller is as below: public function actionCreate() { $model1=new Employee; $model2=new User; if(isset($_POST['Employee']) && isset($_POST['User'])) { $model1->attributes=$_POST['Emoloyee']; $model2->attributes=$_POST['User']; $model1->save(); $model2->save(); echo 'data is saved in both tables'; } $this->render('create',array('model1'=>$model1,model2'=>$mod‌​el2)); } – Yuvraj verma Sep 12 '17 at 08:54
  • You can edit the question and put your code there. It's difficult to read the code in the comments section. – Chinmay Waghmare Sep 12 '17 at 09:12
  • Follow the instruction. This should work https://stackoverflow.com/questions/46173865/how-to-insert-data-to-2-tables-i-e-employee-and-usermigrated-from-single-form – Radhe9254 Sep 12 '17 at 15:24
  • Follow the instruction. This should work https://stackoverflow.com/questions/46173865/how-to-insert-data-to-2-tables-i-e-employee-and-usermigrated-from-single-form – Radhe9254 Sep 12 '17 at 15:26
  • You have a typo in this line `$model1->attributes=$_POST['Emoloyee']; ` Should be `$_POST['Employee'];` – saNs Sep 13 '17 at 08:45

3 Answers3

0

You can try this example,

public function actionCreate()
{
    $model = new Employee(); 
    $user = new User(); 

    if ($model->load(Yii::$app->request->post()) && $user->load(Yii::$app->request->post())) {

        if($model->save() && $user->save()) {
            Yii::$app->session->setFlash('success', 'Record saved successfully.');
        } else {
            //var_dump($model->getErrors());
            //var_dump($user->getErrors());
            Yii::$app->session->setFlash('error', 'Record not saved.');
        }

        return $this->redirect(['index']);
    } else {
        var_dump($model->getErrors());
        var_dump($user->getErrors());
        die();
    }
    return $this->render('create', [
        'model' => $model,
        'user' => $user,
    ]);
}
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
  • thanks for suggestion but this code is also not working. I have migrated user table and also included class in the controller but the data is not inserting in the user table (only in Employee table) what would be the possible way. Acutely in advanced yii2 users can login from user table so I want data from employee table to send to user table also. if there is any other better way so that employee can be created and logged in – Yuvraj verma Sep 12 '17 at 09:24
  • add this on your layout file for you to see output upon success or failure. `=yii\bootstrap\Alert::widget() ?>` – Kalu Sep 12 '17 at 09:32
  • @Yuvrajverma. Check updated answer and it's proper and better way to ensure that model loaded and saved correctly. Refer official yii documentation. – Insane Skull Sep 12 '17 at 10:01
0

could be you have some validation problem

try check this way

    ......

    $model1->attributes=$_POST['Emoloyee']; 
    $model2->attributes=$_POST['User']; 

    if ($model1->validate() && $model2->validate() ) {
        $model1->save(); 
        $model2->save(); 


    } else {
         $errors1 = $model1->errors;
         $errors2 = $model2->errors;
         var_dump($errors1);
         var_dump($errors2);
         exit();

    }

then just for debug try using

    $model1->attributes=$_POST['Emoloyee']; 
    $model2->attributes=$_POST['User']; 
    $model1->save(false);
    $model2->save(false);  

and check in db if the value are saved ..

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I am not getting any validation problem, have migrated user table and also included class in the controller but the data is not inserting in the user table (only in Employee table) what would be the possible way. Acutely in advanced yii2 users can login from user table so I want data from employee table to send to user table also. if there is any other better way so that employee can be created and logged in – Yuvraj verma Sep 12 '17 at 09:31
  • i have update the answer with a suggetsion for test .. let me know – ScaisEdge Sep 12 '17 at 09:33
0

Follow the instruction given in below link . This should work

how to insert data to 2 tables i.e Employee and User(migrated) from single form(Employee Create) and controller in yii2

Radhe9254
  • 198
  • 1
  • 11