How to handle post request

I have a form with 2 differente models, User and Profile. In my controller action I do the following:

$user = new User;


But no attributes are assigned to the model. I thought the data may be wrong or something and I checked with:

$post = Yii::$app->request->post();


Here $post is an array like this


    '_csrf' => '...'

    'Profile' => [

        'attribute' => 'value',

        'attribute2' => 'value2',



    'User' => [

        'attribute' => 'value',

        'attribute2' => 'value2',




So, how do I assign these attributes to their respective model without having to do it one by one?

I guess this is not making any validation before using the save() method because there are some missing attributes that are handled onBeforeSave().



$user->load(Yii::$app->request->post(), 'User');

If the second parameter ($formName) is null it will try to mass assign the whole POST to the model.

But is something about the post( $name = null, $defaultValue = null ) method that I don’t understand.

What does the $name param do? As I understand it’s used to get a specific POST name but looks like it doesn’t.

You can just pass the post value in without the second parameter.


The load method will find the name required from the model.

1 Like