innerjoinwith, select concate

Hello, I use in form.php in the \kartik\widgets\Select2:

    'data' => \yii\helpers\ArrayHelper::map(\models\Person::find()->select(['CONCAT(forname, " ", surname) AS name','person.id'])->innerJoinWith('employee')->orderBy('person.id')->asArray()->all(), 'id', 'name')

it does work proper, but i was said to find some other representation, due to MVC strategy, so i have to substitute it. I have to have something else better in model and not in form.

I am looking for substittutes of functions innerJoinWith(), and select([concat…

it is a fk field in employee for person.id

Thanks in advance.

Hi wslforum,

You may create a static method in Person model:




public static function getPersonList()

{

    $persons = Person::find()->select(['CONCAT(forname, " ", surname) AS name','id'])

        ->orderBy('id')

        ->asArray()

        ->all();

    return ArrayHelper::map($persons, 'id', 'name');

}



Then your view will be a little simpler and cleaner:




'data' => Person::getPersonList(),



BTW, I don’t think you need to inner join Employee in this use case. ;)

Thank you!