答案 0 :(得分:5)
我编写了这个可以添加到User类的函数。
/**
* Finds all users by assignment role
*
* @param \yii\rbac\Role $role
* @return static|null
*/
public static function findByRole($role)
{
return static::find()
->join('LEFT JOIN','auth_assignment','auth_assignment.user_id = id')
->where(['auth_assignment.item_name' => $role->name])
->all();
}
答案 1 :(得分:5)
自Yii版本2.0.7起,它实现的DbManager
和ManagerInterface
具有getUserIdsByRole($roleName)
,无需自定义代码即可执行您想要的操作。
答案 2 :(得分:4)
我使用了@Manquer指南并编写了这个函数:
public static function getRoleUsers($role_name)
{
$connection = \Yii::$app->db;
$connection->open();
$command = $connection->createCommand(
"SELECT * FROM auth_assignment INNER JOIN user ON auth_assignment.user_id = user.id " .
"WHERE auth_assignment.item_name = '" . $role_name . "';");
$users = $command->queryAll();
$connection->close();
return $users;
}
对某人有用。