简体   繁体   English

如何使用yii2在下拉菜单中显示特定的角色用户?

[英]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过滤的用户。 user_type_id=13 means the users are promoters. user_type_id=13表示用户是发起人。

What i want? 我想要的是? I want to show the users like below sql query using ArrayHelper::map . 我想使用ArrayHelper::map向用户显示以下SQL查询。

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 : 由于您使用的是mysql,因此可以简单地使用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: 如果在useruser_type之间存在关系,则可以改用joinWith ,它为您处理joinWith类型和joinWith条件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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