How To Insert Multiple Rows In After Save Method Using Batchinsert

hi i have two tables ,one is user and other is company so when user created same time his some info should go into company tables also

i have so much info that i want to insert into company table so in user model aftersave method i can use

so plz can any one tell me how to use aftersave method and use batchinsert in that method

table company

account name,

type

table user

name

when user created with name abc

i have array for every user

array[ Accounts Payable -abc,personal],

 [ 	Accounts cash -abc ,salar],


 [ 	Accounts check -abc,bank]

i want to insert this array into company table in acount name and type fields in company table

Hi,

I think you want to approach something like this. Store values in other models after saving the User model.




public function actionCreate()

    {

        $model = new User;

        $accountsPayable = new AccountsPayable;

        $accountsCash = new AccountsCash;

        $accountsCheck = new AccountsCheck;


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

        && $accountsCash->load(Yii::$app->request->post()) && $accountsCheck->load(Yii::$app->request->post())) {


            if($model->save()){

                //Save user ID in other three models

                $accountsPayable->user_id = $model->id;

                $accountsCash->user_id = $model->id;

                $accountsCheck->user_id = $model->id;


                //Store user ID & posted values

                $accountsPayable->save();

                $accountsCash->save();

                $accountsCheck->save();


                return $this->redirect(['view', 'id' => $model->id]);

            

            }

        } else {


            return $this->render('create', [

                'model' => $model,

                'accountsPayable' => $accountsPayable,

                'accountsCash' => $accountsCash,

                'accountsCheck' => $accountsCheck,

            ]);

        }

    }



So, in your Create form, you can use the variables: $accountsPayable, $accountsCash and $accountsCheck to fill the fields you need for those models in the same form as User

I hope this helps you.

thnku for this also,it will help me somewhere in my project

i want to insert 123 rows of data in company table when user model is saved so i want to do in aftersave in user model