可能的“简单”解决方案:
代码是正确的,它应该把你的$userId
and$groupId
放在数据库中,以准确地放在#__user_usergroup_map
.
顺便说一句,如果您使用错误,则认为此方法会引发错误,groupId
但如果您插入错误,则不会引发任何错误$userId
,而对于错误,我的意思是它不存在。
所以有些用户$userId = 358;
不存在的canches。
更新 - 硬调试:
好的,在这种情况下,我建议您挖掘助手的代码。
该文件是:
libraries/joomla/user/helper.php
在第 33 行你有JUserHelper::addUserToGroup
.
这是代码:
public static function addUserToGroup($userId, $groupId)
{
// Get the user object.
$user = new JUser((int) $userId);
// Add the user to the group if necessary.
if (!in_array($groupId, $user->groups))
{
// Get the title of the group.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__usergroups'))
->where($db->quoteName('id') . ' = ' . (int) $groupId);
$db->setQuery($query);
$title = $db->loadResult();
// If the group does not exist, return an exception.
if (!$title)
{
throw new RuntimeException('Access Usergroup Invalid');
}
// Add the group data to the user object.
$user->groups[$title] = $groupId;
// Store the user object.
$user->save();
}
if (session_id())
{
// Set the group data for any preloaded user objects.
$temp = JFactory::getUser((int) $userId);
$temp->groups = $user->groups;
// Set the group data for the user object in the session.
$temp = JFactory::getUser();
if ($temp->id == $userId)
{
$temp->groups = $user->groups;
}
}
return true;
}
保存组的位是$user->save();
。
尝试var_dump()
直到那里,看看问题出在哪里。