【问题标题】:joomla - Storing user parameters in custom component issuejoomla - 在自定义组件问题中存储用户参数
【发布时间】:2013-09-10 19:11:01
【问题描述】:

您好,对于我的自定义组件,我需要为 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;
    }

这段代码用于存储与用户表相关的数据。

【问题讨论】:

  • 我已经对此进行了测试,它似乎工作正常。您是否尝试为有效用户存储此信息(即,JRequest::getVar('id','0') 返回有效用户 ID - 0 通常不是有效 ID)?如果用户无效,则存储数据可能会出错。您可以尝试通过执行echo 'id is '.JRequest::getVar('id','0').' and error is: '.$user->getError(); 来获得一些洞察力
  • 对不起,我犯了一些错误,上面的代码 Jrequest 是用于管理页面的,它采用正确的值,因为重定向后单选按钮检查状态正在改变,并在前端查看状态我得到没有任何 id 的 JUser 来获取当前用户。然后我正在检查它是否以 $user->guest == 0 身份登录,如果它是真的,它写在页面上但旧值。
  • 请显示存储数据的部分,无论它是否正常工作。您应该让人们尽可能轻松地帮助您解决问题。请通过编辑您的原始问题来做到这一点。
  • MasterAM 感谢您的关注 :)
  • 如果我是你,我会将我的 Joomla 安装调试模式设置为 on 以便查看所有查询并使用我最喜欢的调试器来单步调试代码并查看加载了什么,什么是跳过以及为什么。

标签: database joomla parameters components


【解决方案1】:

事实证明,Joomla 将 JUser 实例存储在导致问题的会话中。

从后端更改用户的参数时,更改不会反映在该用户的会话中,直到她注销并重新登录。

我们找不到一个简单的选项来修改另一个用户的活动会话,所以我们求助于使用一个插件来刷新登录用户会话中的 JUser 实例,如下所示:

$user = JFactory::getUser();
$session = JFactory::getSession();

if(!$user->guest) {
    $session->set('user', new JUser($user->id));
}

(参考:here)。

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 2013-07-31
    • 2013-09-10
    • 2021-08-03
    • 2017-07-21
    • 2013-03-01
    • 2018-01-06
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多