如何在 Joomla 2.5 中获取用户组名称 [英] How to Get User Group Names in Joomla 2.5

查看:30
本文介绍了如何在 Joomla 2.5 中获取用户组名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个在 Joomla 1.7 中开发的 Joomla 2.5 组件.我一直在使用这样的代码:

I'm writing a Joomla 2.5 component that I had been developing in Joomla 1.7. I have been using code like this:

$user = JFactory::getUser();
$groups = $user->get('groups');

$groups 数组将包含一个以组名作为索引的 id 列表.Joomla 2.5 似乎取消了这个功能.我一直无法找到如何在不直接查询数据库的情况下获取组名.有没有什么方法可以获取用户所属组的列表,而不必求助于查询数据库?

The $groups array would contain a list of ids with the group name as the index. Joomla 2.5 seems to have scrapped this functionality. I have been unable to find out how to get the group names without directly querying the database. Is there any method for getting a list of the groups a user is a member of without having to resort to querying the database?

推荐答案

我在下面生成的代码获取用户所属的所有组的名称并将它们存储在变量 $groupNames由换行符分隔:

The code I generated below gets the names of all the groups the user is a part of and stores them in the variable $groupNames separated by line breaks:

foreach ($user->groups as $groupId => $value){
    $db = JFactory::getDbo();
    $db->setQuery(
        'SELECT `title`' .
        ' FROM `#__usergroups`' .
        ' WHERE `id` = '. (int) $groupId
    );
    $groupNames .= $db->loadResult();
    $groupNames .= '<br/>';
}
print $groupNames;

它在技术上查询数据库,但通过 Joomla API 完成.这对我来说在 Joomla 2.5 上运行良好.

It technically queries the database but is done via the Joomla API. This is working well for me on Joomla 2.5.

这篇关于如何在 Joomla 2.5 中获取用户组名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆