[英]How to show the particular role users in dropdown using yii2?
'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: 如果在
user
和user_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.