I do have this code to add an extra field to an array: all the names of users. It should work with toArray. But toArray is not available, only asArray. But asArray returns an empty array. So I do not get the concept of extraFields, I think. What is the differecent between toArray and asArray? Why are they different?
public function actionIndex()
{
$spellen = Spelrondeactievespelers::find()
->where(['tbl_spelrondeactievespelers.user_id' => Yii::$app->user->id])
->select(['tbl_spelrondeactievespelers.spelrondeactievespelers_id', 'tbl_spelrondeactievespelers.spelronde_id','tbl_spelvorm.spelvormnaam'])
->joinWith('spelvorm')
->asArray([], ['namen'])
->all();
return $this->render('index', [
'spellen' => $spellen,
]);
}
public function extraFields()
{
return [
'namen'=>function() {
$spelers = Spelrondeactievespelers::find()
->where(['spelronde_id' => $spelronde])
->select(['spelrondeactievespelers_id','profile.user_id','name'])
->joinWith('profile')
->asArray()
->all();
$namen = [];
foreach($spelers as $speler) {
$namen[]= $speler->name;
};
return implode(', ', $namen);
}
];
}