Multi model form

I need a simple yii2 application using yii2 advance template while I am still learning it

I need to add 2 more tables user_profile and user_profile_type

user table

id, email, username, password,etc…


user_profile_type will have column id and label

1, First Name

2,Last Name

and user_profile will have column

id, user_id, user_type_id, value



i should be able to view the user with it’s first name and last name and also should be able to update it from user edit screen.

can any one guide me how to do this so I can update 2 models from one form as the auto generated CRUD using gii allows me to display and edit one model only?



There is a really simple explanation on how to do that here:

Thanks Dubby but my profile data store in row by row and not columns. Also Userprofile table poitns to UserProfileType table to get the label as well.

So, if I understand, on form submission you’re trying to save one record in the user table, two records in the user_profile_type table and two rows in the user_profile table?

Although I think that can be done I think it would be rather awkward. I would question whether you are approaching the application requirements the right.

Anyway, you could set the form fields as:

<input name="user[username]">

<input name="user[email]">

<input name="user[password">

<input name="user_profile[first_name][name]">

<input name="user_profile[last_name][name]">

And handle them from there following the example in the link earlier but manually setting the attributes.

$user_profile = new UserProfile;

$user_profile->first_name = Yii::app()->getRequest()->getParam('user_profile')['first_name'];

$user_profile->last_name = Yii::app()->getRequest()->getParam('user_profile')['last_name'];

I think that’s it. I haven’t tested it.

Is there a good reason that you don’t have first name and last name in the user_profile table? or at least have them both in one other table as two different fields?

I am trying to build generic model so i can add new profile field in user_profile_type which will be referred by user_profile table which hold uid and user_profile_type->id and value for that record.

so I can store in user_profile table

uid=1, user_type_id=1, value=nik

uid=1, user_type_id=2, value=niklastname

Yes, sorry. I should look more like this:

<input name="user[username]">

<input name="user[email]">

<input name="user[password">

<input name="user_profile[first_name]">

<input name="user_profile[last_name]">

$user = new User;



$user_profile = new UserProfile;

$user_profile->id = $user->id;

$user_profile->type = 1;

$user_profile->value = Yii::app()->getRequest()->getParam('user_profile')['first_name'];


$user_profile = new UserProfile;

$user_profile->id = $user->id;

$user_profile->type = 2;

$user_profile->value = Yii::app()->getRequest()->getParam('user_profile')['last_name'];


You’ll need to add validation of course.

how my model should look like? any special code except crud generated?

Just as you would think. The best way to learn here is to build it yourself and see pros and cons of different methods.

thanks for your guidance. I have updated my structure so it works correctly.