How to fetch relational database values using extraFields

My table:

user is Master table

class_detail , userinfo are child tables with foreign key of user table.

I am trying to return JSON data from my REST. so i am using extraFields().

 $user = ClassDetail::findOne($id);

     // user in second parameter array is extarfields

$data['USER'] = $user->toArray([],['user]);

Its working.

But, i trying to return user info details also,i cant find a solution

 $user = ClassDetail::findOne($id);

$data['USER'] = $user->toArray([],['user', 'user.userinfo']);

Below code is working , but i need to return like above statement, thats only match my requirements

My requirement of loading data in table like : Child->Parent->Child


$user = User::findOne($id);

$data['USER'] = $user->toArray([],['classDetail', 'userinfo']);

Try something like this

$user = ClassDetail::findOne($id);

$list = yii\helpers\ArrayHelper::getValue($user->classDetail, function ($classDetail) {

	return $classDetail->userinfo;


Thanks , But i need it in single array .so i modify to fields.

public function fields(){

    $fields['classDetail'] = function($this){


            return $classDetail->userinfo;


        return null.



Since I hit this topic searching for same, here is the solution.
The second param defines which “attributes” shall be expanded.

So like this:

$user = ClassDetail::findOne($id); 
$data['USER'] = $user->toArray([],['user.userinfo', 'user']);