Concatenate strings in Yii2 ArrayHelper::map

Assuming the Contact model get Name and Surname.

How will I get the concatened fullName in ArrayHelper?




public function getfullName()

        {

                return $this->name.' '.$this->surname;

        }



with Yii1… ok




CHtml::listData($models, 'id', 'fullName');




with Yii2 ?




ArrayHelper::map(app\models\base\Contact::find()->asArray()->all(), 'ID', 'fullName')






ArrayHelper::map(app\models\base\Contact::find()->all(), 'ID', 'fullName')



No need to do anything again this is working that said with Yii2 and that should be considered like tips.

In model




public function getfullName()

        {

                return $this->name.' '.$this->surname;

        }



And in (view) _form.php for example




            echo $form->field($model, 'ContactID')->widget(Select2::classname(), [

                'data' => ArrayHelper::map(\app\models\Contact::find()->orderBy('Nom')->all(), 'ID', 'fullName'),

                'options' => ['placeholder' => 'Choisir une personne'],

                'pluginOptions' => [

                    'allowClear' => true

                ],

            ]);



You’re right, I haven’t even noticed the “asArray” in my first post. so that was the error




ArrayHelper::map(app\models\base\Contact::find()->asArray()->all(), 'ID', 'fullName')



I have a similar issue, Just that my problems is with joins:

This is mi "_form":




ArrayHelper::map(Order::find()->joinWith('compartmentIdCompartment')->joinWith('hybridIdHybr')->orderBy('idorder')->asArray()->all(), 'idorder', 'createOrder'),



and this is my function in the model:





    public function createOrder()

    {

        $genial = 'Compartment: '.$this->compartmentIdCompartment->compNum.' Hybrid: '.$this->hybridIdHybr->hybrid;

        return $genial;

    }



And the result is blank.