Yii的 - 操纵一个sesssion变量 [英] Yii - Manipulating a sesssion variable

查看:139
本文介绍了Yii的 - 操纵一个sesssion变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是还是一个新手,当涉及到使用Yii,但我一直与会话变量工作在过去的几天里,我似乎无法把握后面我的错误的概念。任何意见将是AP preciated。

I am a still a newbie when it comes to using YII, but I been working with session variables for the past few days, and I can't seem to grasp to the concept behind my error. Any advice will be appreciated.

我的附加功能,完美的作品,到目前为止,我当前保持添加到我的会话变量营养的最后3个变量的轨道的目的。

My add function works perfectly so far, for my current purpose of keeping track of the last 3 variables added to my session variable nutrition.

public function addSessionFavourite($pageId)
{
    $page = Page::model()->findByPk($pageId);
    $categoryName = $page->getCategoryNames();

    if($categoryName[0] == 'Nutrition')
    {
        if(!isset(Yii::app()->session['nutrition']))
        {
            Yii::app()->session['nutrition'] = array();
        }
        $nutrition = Yii::app()->session['nutrition'];
        array_unshift($nutrition, $pageId);
        array_splice($nutrition, 3);
        Yii::app()->session['nutrition'] = $nutrition;
    }

我删除的功能似乎并没有在所有的工作,不管是什么我尝试用它做。为什么我转化中的会话数组到临时数组的原因是试图绕过如果全球化的变量未设置()函数里面,只是局部变量被销毁,而在调用环境中的变量将保留相同的值未设置()之前被称为。但它是一个完全的失败。

My remove function doesn't seem to work at all, no matter what I try to do with it. The reason why I am transfering the session array to a temp array was to try to get around the "If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called." But it was a total failure.

public function removeSessionFavourite($pageId)
{
    $page = Page::model()->findByPk($pageId);
    $categoryName = $page->getCategoryNames();

    if($categoryName[0] == 'Nutrition')
    {
        if(!isset(Yii::app()->session['nutrition']))
        {
            return true;
        }
        $nutritionArray = Yii::app()->session['nutrition'];
        unset($nutritionArray[$pageId]);
        Yii::app()->session['nutrition'] = $nutritionArray;
    }

任何意见或朝向推到正确的方向将是AP preciated。

Any advice or push toward to the correct direction will be appreciated.

推荐答案

我个人而言,我从来没有使用的Yii ::应用() - GT;会议我通常使用的Yii的用户,我从未有过的任何问题与它:

I personally I have never used Yii::app()->session I normally use the Yii user and I have never had any issues with it:

Yii::app()->user->setState('test', array('a'=>1,'b'=>2));
print_r(Yii::app()->user->getState('test')); //see whole array

$test = Yii::app()->user->getState('test');
unset($test['b']);
Yii::app()->user->setState('test',$test);
print_r(Yii::app()->user->getState('test')); //only 'a'=>1 remains

Yii::app()->user->setState('test', null);
print_r(Yii::app()->user->getState('test')); //now a null value

当我加入了注释上面似乎有与多维数组与会话变量的问题:的 HTTPS://$c$c.google.com/p/yii/issues/detail ID = 1681

这篇关于Yii的 - 操纵一个sesssion变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆