Loading relation into model and keeping expand feature issue

Hi everyone,

I am building a REST API and I have a model that defines a relation and the extrafield, so I can use the expand feature.

class MainModel extends ActiveRecord
{
...

public function getChildren()
{
    
    return $this->hasMany(ChildModel::className(), ['main_id' => 'id']);  
}

public function extraFields() 
{
    return ['children'];    
}

...
}

I can use controller/view&id=12345&expand=children as this is one of my main goals.

The other goal I have is to create a custom controller action that will receive and load the full model data (data + relation data):

Received data example:

{
"mainfield": "mainfield",
"mainfield2": "mainfield2",

"children": [
	{
		"childfield1": "childfield1",
		"childfield2": "childfield2"
	},
	{
		"childfield1": "childfield1",
		"childfield2": "childfield2"
	}
]
}

Controller action:

public function actionCustom(){

$model = new MainModel();
if ($model->load(Yii::$app->request->post(), '') && $model->validate()){
	if($model->save())
		return $model;
	else
		throw new \yii\web\BadRequestHttpException();
}
else
	throw new \yii\web\BadRequestHttpException();
}

This is where I’m struggling because I need to use the same name in the relation definition and received data.

I tried adding an attribute to the model like $children and configuring it as a safe attribute but then expand will not work.

What should be the best approach to achieve this? I would really appreciate your help.

BR