Nested relations

I have a [font="Courier New"]User[/font] model which includes [font="Courier New"]Profile[/font] which further includes [font="Courier New"]Country[/font].

I have a REST call like this:


GET api/users?expand=profile

Which returns all users with profile as expected. But I also need Country included in my profile. Something like this:


GET api/users?expand=profile,profile.country

But this does not work. Is this supported at all, or are there any other solutions oir such cases?

I’ve also tried overriding [font=“Courier New”]actionIndex[/font] to manually find all records with relations:


public function actionIndex() {

    $query = User::find()->with('profile')->all();


    return new ActiveDataProvider([

        'query' => $query,

    ]);

  }

But this does not even include [font=“Courier New”]Profile[/font]. API JSON response only shows basic users with no profile. I haven’t even tried Country. Any ideas what am I doing wrong?

Relation expansion is managed by yii\rest\Serializer. Here’s how it’s done for one model: https://github.com/yiisoft/yii2/blob/cd8c01f555e425d4654775002ae5f44152e0c4b1/framework/rest/Serializer.php#L239 and here’s for collection of models: https://github.com/yiisoft/yii2/blob/cd8c01f555e425d4654775002ae5f44152e0c4b1/framework/rest/Serializer.php#L273

So it’s basically calls the Model::toArray method which is implemented in ArrayableTrait: https://github.com/yiisoft/yii2/blob/2f103ac5aa7bab6a38f00c24317a0662146cd4e6/framework/base/ArrayableTrait.php#L116

An easy option would be include the country relation in your Profile::fields.