【问题标题】:joomla not showing full map of user group phpjoomla没有显示用户组php的完整地图
【发布时间】:2013-06-07 17:46:25
【问题描述】:

所以我从后端在 Joomla 中创建了一些自定义用户组。 我将注册用户放在这些自定义组下。 组是这样的-

公开
|— 用户
|—|— 审核员
|—|— Custom_group1
|—|—|— Custom_group2
|—|—|—|— Custom_group3
|—|—|—|—|— 管理员
|—|—|—|—|—|— 超级用户
|—|— Custom_group4

所以,假设user_bCustom_group4 组和Custom_group3 下,其中自定义组3 和4 的ID 分别是2 和11(比如说), MySql 数据库看起来像-

表:#__users

id       name           username           email               password     
523     User A          user_a          user_a@mysite.com      blablabla...
524     User B          user_b          user_b@mysite.com      blablabla... 

表:#__user_usergroup_map

user_id(Foreign Key to #__users.id)       group_id(Foreign Key to #__usergroups.id)
    523                                       8
    524                                       2
    524                                       11

您可以看到为用户 ID 524 分配了两个组。 现在我有这个 PHP 代码来获取用户的组 id(用户 id-524)

        $user = JFactory::getUser();
        $userId = $user->id;
        $db = JFactory::getDbo();
        $recursive = False;

        // Build the database query to get the rules for the asset.
        $query  = $db->getQuery(true);
        $query->select('ug.group_id');
        $query->from('#__user_usergroup_map AS ug');
        $query->leftJoin('#__users AS a ON a.id = ug.user_id');
        $query->where('a.id = '.(int) $userId);

        // Execute the query and load the rules from the result.
        $db->setQuery($query);
        $result = $db->loadResult();
        print_r((array)$result);

其中,我得到了输出-

Array([0] => 2)

但我希望它是Array([0] => 2, [1]=> 11)

谁能帮帮我?

【问题讨论】:

    标签: php mysql joomla


    【解决方案1】:

    解决了。

    我查看并找到了一个类似的函数here,使用了类似的查询(比预期的更好)。

    这是该代码的一部分-

            $query  = $db->getQuery(true);
            $query->select($recursive ? 'b.id' : 'a.id');
            $query->from('#__user_usergroup_map AS map');
            $query->where('map.user_id = '.(int) $userId);
            $query->leftJoin('#__usergroups AS a ON a.id = map.group_id');
    
            // If we want the rules cascading up to the global asset node we need a self-join.
            if ($recursive) {
                    $query->leftJoin('#__usergroups AS b ON b.lft <= a.lft AND b.rgt >= a.rgt');
            }
    
            // Execute the query and load the rules from the result.
            $db->setQuery($query);
            $result = $db->loadResultArray();
    

    唯一的区别在于获取表信息的方式。

    我现在可以简单地使用它——

    jimport( 'joomla.access.access' );
    $groups = JAccess::getGroupsByUser(526, False); //526 is the user_id for user b
    print_r($groups);
    

    这就是我想要的-

    Array ( [0] => 2 [1] => 11 )
    

    感谢谷歌。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多