简体   繁体   中英

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

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';

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 (),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM