When new user registering how to save additional information to different table?

When a new user registering all the details are stored under user table, but i have to save additional information such as first name, last name…etc. into another table (user_details) how to do that? user_details has the foreign key of users’ table primary key

Hi Ahamed, You should do like this to update the relational model data.


$model = new User;

$model->load(Yii::$app->request->post()['User']);

if($model->validate() && $model->save()) {

    $modelUserDetails = new UserDetails;

    $modelUserDetails->load(Yii::$app->request->post()['UserDetails']);

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

    if($modelUserDetails->validate() && $modelUserDetails->save()) {

        // Success

    }

}

I would recommend creating a form model to work with multiple models.

I found an article recently that has a really good example of this.

https://mrphp.com.au/blog/advanced-multi-model-forms-yii2/