您好我的自定义组件我需要为joomla用户设置一些自定义参数,以便检查用户是否在试用期间,并且可以从特定用户的组件管理面板进行更改。
检索参数时出现问题。我认为它存储在cookie中,并且没有更新。我写了这样的代码来检查它。
$user = JFactory::getUser(JRequest::getVar('id','0'));
echo $user->getParam('trialPeriod','0');
保存我正在使用的值JHTML booleanlist。
$user->setParam('trialPeriod',$data['trialPeriod']);
$user->save();
然后将值存储在该用户行中的joomla users表中,其中params列为;
{"trialPeriod":"0"}
在这种情况下,它将值回显为0.然后我将trialPeriod var的状态改为1并存储在db中,它将db更新为;
{"trialPeriod":"1"}
毕竟我刷新了值为提示的页面,屏幕上的值仍然与0相同;
澄清;
首先,保存参数正确更改没有问题。问题是检索更改的一个。相关代码如下;
$user = JFactory::getUser();
$doc = JFactory::getDocument();
if($user->getParam('trialPeriod',0) == 0){
$ed = JFactory::getDate($obj->expirationDate);//obj is user from custom table and there is no problem with getting it.
$isTrialEnd = FALSE;
}else{
$ed = JFactory::getDate($user->getParam('trialExp',0));
$isTrialEnd = TRUE;
}
if($isTrialEnd){
//do something else
}else{
echo $user->getParam('trialPeriod','0');
}
实际上大部分代码都不需要解释,但你会明白这一点。
这是什么解决方案?
Editted。
$app = JFactory::getApplication();
$config = JFactory::getConfig();
$db = $this->getDbo();
$isNew = empty($data['uid']) ? true : false;
$params = JComponentHelper::getParams('com_dratransport');
if($isNew){
// Initialise the table with JUser.
$user = new JUser;
// Prepare the data for the user object.
$username = self::getCreatedUserName($data['type']);
$data['username'] = !empty($data['username']) ? $data['username'] : $username;
$data['password'] = $data['password1'];
$useractivation = $params->get('useractivation');
// Check if the user needs to activate their account.
if (($useractivation == 1) || ($useractivation == 2)) {
$data['activation'] = JApplication::getHash(JUserHelper::genRandomPassword());
$data['block'] = 1;
}
}else{
$user = JFactory::getUser($data['uid']);
$data['password'] = $data['password1'];
}
$membership = DraTransportHelperArrays::membershipCFG();
$membership = $membership[$data['membership']];
if($data['membership'] == 4)
$data['groups'] = array($params->get('new_usertype',2),$params->get($membership,2));
else
$data['groups'] = array($params->get($membership,2));
$data['name'] = $data['companyName'];
$user->setParam('trialPeriod',$data['trialPeriod']);
// Bind the data.
if (!$user->bind($data)) {
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_BIND_FAILED', $user->getError()));
return false;
}
// Load the users plugin group.
JPluginHelper::importPlugin('user');
// Store the data.
if (!$user->save()) {
$app->enqueuemessage($user->getError());
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $user->getError()));
return false;
}
这段代码用于存储与users表相关的数据。
答案 0 :(得分:1)
事实证明,这是Joomla在导致问题的会话中存储JUser
实例的事实。
从后端更改用户参数时,更改不会反映在该用户的会话中,直到她退出并重新登录。
我们找不到修改anther用户活动会话的简单选项,因此我们使用了一个插件来刷新登录用户会话中的JUser实例,如下所示:
$user = JFactory::getUser();
$session = JFactory::getSession();
if(!$user->guest) {
$session->set('user', new JUser($user->id));
}
(参考:here)。