Zend_Session_Namespace未按预期工作

时间:2011-03-31 14:19:22

标签: php zend-framework session

创建命名空间后,如果我尝试向其添加值,则该变量具有特定值,但不具有命名空间的会话。但是,如果我创建一个变量,这是一个数组的键,并将值添加到它,它将保存到会话中。为了说明我的观点:

public $submitUser;
public function init ()
{
    $this->submitUser = new Zend_Session_Namespace('submitUser');
    Zend_Session::rememberMe(60*60*24*7);
}

public function selectUserAccount ($username)
{        
    $returnArray = array(
        'name' => 'Man', 
        'surname' => 'With a surname',
        'username' => $username
    );
    $this->submitUser->user = $returnArray;
}

插入会话

'submitUser' => 
    array
      'user' => 
        array
          'name' => string 'Man' (length=3)
          'surname' => string 'With a Surname' (length=14)
          'username' => string 'jpeiseni' (length=8)

但是

public function selectUserAccount ($username)
{        
    $returnArray = array(
        'name' => 'Man', 
        'surname' => 'With a Surname',
        'username' => $username
    );
    $this->submitUser = $returnArray;
}

不在会话中设置变量

这是一个小麻烦,我可能会忍受,但我想知道是否有理由不允许namaspace自己有价值,或者我错过了什么?

2 个答案:

答案 0 :(得分:4)

是的,这是有道理的,你实际做的是:

$this->submitUser = new Zend_Session_Namespace('submitUser');
$this->submitUser = $returnArray;

所以你创建一个Zend_Session_Namespace的实例,然后用一个完全不同的变量替换它。

也许这样的事情会更有意义(取决于这段代码的用途):

$this->submit = new Zend_Session_Namespace('submit');
$this->submit->user = $returnArray;

答案 1 :(得分:3)

试试这个:

public function selectUserAccount ($username)
{
    $returnArray = array(
        'name' => 'Man',
        'surname' => 'With a Surname',
        'username' => $username
    );
    $this->submitUser->applySet('array_merge', $returnArray);
}