嗨,对于我的自定义组件,我需要为 joomla 用户设置一些自定义参数,以检查用户是否有试用期,并且可以从特定用户的组件管理面板中更改它。
检索参数时出现问题。我认为它存储在 cookie 中并且没有更新。我写了这样的代码来检查它。
$user = JFactory::getUser(JRequest::getVar('id','0'));
echo $user->getParam('trialPeriod','0');
保存我使用 JHTML booleanlist 的值。
$user->setParam('trialPeriod',$data['trialPeriod']);
$user->save();
然后将值存储在该用户所在行的 joomla 用户表中,参数列为;
{"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');
}
实际上大部分代码不需要解释它,但你会明白的。
解决方案是什么?
已编辑。
$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 表相关的数据。