Answer the question
In order to leave comments, you need to log in
How to add related model to ArrayHelper::toArray()?
There was a problem, you need to give data with related data in the model with getters:
If you do it through asArray ()
return Project::find()->with(['group', 'userShare', 'userShare.profile', 'author'])->where(['id' => $id])->asArray()->one();
$project = Project::find()->with(['group', 'userShare', 'userShare.profile', 'author'])->where(['id' => $id])->one();
return $project->toArray([], ['group', 'userShare', 'userShare.profile', 'author']);
Answer the question
In order to leave comments, you need to log in
Thank you, everything worked out, I'll leave the solution here, suddenly it will be useful to someone:
$project = Project::find()->with(['group', 'userShare', 'author'])->where(['id' => $id])->one();
return ArrayHelper::toArray($project, ['app\models\Project' => array_merge($project->fields(), ['group',
'userShare' => function ($project) {
$share = $project->userShare;
$share = array_map(function($elem) {
$elem->profile = $elem->profileq;
return $elem;
}, $share);
return $share;
},
'author'])]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question