Zend_Session中的节点不再存在错误

时间:2009-07-05 15:20:39

标签: php zend-framework session

您好我的会话使用Zend Framework 1.7.6。

当我尝试将数组存储到会话时存在问题,会话命名空间也存储其他用户数据。

我目前在stacktrace中收到以下消息

Fatal error: Uncaught exception 'Zend_Session_Exception' with message 'Zend_Session::start() - 
...

Error #2 session_start() [function.session-start]: Node no longer exists Array 

我认为这是错误的代码是:

//now we add the user object to the session
    $usersession = new Zend_Session_Namespace('userdata');
    $usersession->user = $user;

    //we now get the users menu map        
    $menuMap = $this->processMenuMap($menuMapPath);

    $usersession->menus = $menuMap;

自从尝试将数组添加到会话命名空间后,此错误才开始出现。

任何想法可能导致节点不再存在数组消息?

非常感谢

2 个答案:

答案 0 :(得分:3)

您是否尝试在会话数据中存储SimpleXML对象或其他与libxml相关的内容? 这不起作用,因为在session_start()期间对对象进行反序列化时,不会恢复基础DOM树。而是存储xml文档(作为字符串)。

你可以实现这一点,例如提供"magic functions" __sleep() and __wakeup()。但是__sleep()必须返回一个数组,其中包含要序列化的所有属性的名称。如果添加另一个属性,则还必须更改该数组。这消除了一些自动化......

但是如果你的menumap类只有几个属性,那么对你来说可能是可行的。

<?php
class MenuMap {
    protected $simplexml = null;
    protected $xmlstring = null;

    public function __construct(SimpleXMLElement $x) {
        $this->simplexml = $x;
    }

    public function __sleep() {
        $this->xmlstring = $this->simplexml->asXML(); 
        return array('xmlstring');
    }   

    public function __wakeup() {
        $this->simplexml = new SimpleXMLElement($this->xmlstring);
        $this->xmlstring = null;
    }

    // ...
}

答案 1 :(得分:1)

您应该在会话中存储XML字符串。或者,您可以围绕该XML字符串创建一个包装类:

在这些方法中,您可以关注对象的状态。

相关问题