如何在Joomla 2.5中获取用户组名称 [英] How to Get User Group Names in 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屋!