bambinou
(Benoitrivaux)
1
Hello,
I have this code:
<?= $form->field($model, 'author_id')->dropDownList(ArrayHelper::map(Author::find()->select(['firstname','surname','id'])->all(), 'id', 'firstname'),['class' => 'form-control inline-block']); ?>
I cannot manage to have the first name + surname in the dropdown menu.
I tried this below but it gives and error, any idea how to pass both please, thank you:
<?= $form->field($model, 'author_id')->dropDownList(ArrayHelper::map(Author::find()->select(['firstname','surname','id'])->all(), 'id', 'firstname' . 'surname'),['class' => 'form-control inline-block']); ?>
Ben
Vojtech
(Vojtech Horak)
2
Use ArrayHelper::map passing as third parameter a function.
$ah = ArrayHelper::map(
Author::find()->select(['firstname','surname','id'])->all(),
'id',
function($data) {
return $data['firstname'].' '.$data['surname'];
}
);
<?= $form->field($model, 'author_id')->dropDownList($ah,['class' => 'form-control inline-block']); ?>
bambinou
(Benoitrivaux)
4
Hi Thank you both you your reply.
Fabrizio, would you add the
$ah = ArrayHelper::map(
Author::find()->select(['firstname','surname','id'])->all(),
'id',
function($data) {
return $data['firstname'].' '.$data['surname'];
}
);
In the view file? or in the model?
Thank you,
Ben
In view. I’ve splitted $ah definition from form dropDownList only to make clearer code.