How to show the particular role users in dropdown using yii2?

194 views Asked by At

This is my view

'promoter_id' => [ 
    'type' => Form::INPUT_DROPDOWN_LIST,
    'options' => ['prompt' => '--'.Yii::t ( 'app', 'Ventor Type' ).'--',],
    'items' => ArrayHelper::map (
        User::find ()->orderBy ( 'username')->where(['user_type_id' => [13]])->asArray ()->all (),
        'id', 
        'username' 
    )
]

What i did? show the users filtered by user_type_id = 13. user_type_id=13 means the users are promoters.

What i want? I want to show the users like below sql query using ArrayHelper::map.

SELECT u.username 
    FROM tbl_user u,tbl_user_type t
    where u.user_type_id = t.id and t.type='promoter';
1

There are 1 answers

0
topher On BEST ANSWER

Since you are using mysql you can simply use an innerJoin:

User::find()
    ->select(['user.id', 'user.username'])
    ->innerJoin('user_type', 'user_type.id = user.id')
    ->where(['user_type.type' => 'promoter'])
    ->orderBy('username')
    ->asArray()
    ->all(),

If you have a relation between user and user_type you can use joinWith instead, which handles the join type and the join condition for you:

User::find()
    ->select(['user.id', 'user.username'])
    ->joinWith('userType', true)
    ->where(['userType.type' => 'promoter'])
    ->orderBy('username')
    ->asArray ()
    ->all (),