How to pass first name and surname in dropdown list

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

You can use this approach: http://www.yiiframework.com/forum/index.php/topic/62465-merge-firstname-and-surname-into-one-string-active-record-find/page__view__findpost__p__276494

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']); ?>



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.

Got it Thanks Fabrizio :slight_smile:

Ben