无法找到CodeIgniter会话对象

时间:2010-06-04 19:52:36

标签: php codeigniter

我正在开发一个跟踪用户数据的模型,并在适当的时候将其存储在会话中。这是它的基本结构:

public function __construct() {
    parent::__construct();

    $this->load->database();
    $this->load->library('session');

    if (!is_null($this->session->userdata('user'))) {
        $arrData = $this->session->userdata('user');

        $this->_netID = $arrData['_netID'];
        $this->_fName = $arrData['_fName'];
        $this->_eventMgr = $arrData['_eventMgr'];
        $this->_accessMgr = $arrData['_accessMgr'];
        $this->_accessAcnt = $arrData['_accessAcnt'];
        $this->_accessEvnt = $arrData['_accessEvnt'];
        $this->_accessCMS = $arrData['_accessCMS'];
        $this->_accessReg = $arrData['_accessReg'];
        $this->_accessRep = $arrData['_accessRep'];
        $this->_accessPay = $arrData['_accessPay'];
        $this->_okEvents = $arrData['_okEvents'];
    }
}
public function __get($name) {
    switch($name) {
            default:
            if (function_exists('parent::__get')) {
                return parent::__get($name);
            } else {
                return $this->$name;
            }
    }
}
public function __set($name,$val) {
    switch($name) {
            default:
            if (function_exists('parent::__set')) {
                return parent::__set($name,$val);
            } else {
                 $this->$name = $val;               }
    }
}

我遇到的问题在构造函数中是正确的。每当它到达第七行(检查用户密钥是否为空)时它就会出错,说:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: SessionUser::$session

Filename: models/sessionuser.php

Line Number: 83

Fatal error: Call to a member function userdata() on a non-object in /usr/cwis/data/www-data/melioraweekenddev/system/application/models/sessionuser.php on line 38

关于为什么的任何想法?

1 个答案:

答案 0 :(得分:3)

我的猜测是你正在尝试从模型中加载一个不可能的模型库。 请尝试改为:

public function __construct() {
    parent::__construct();

    $CI =& get_instance(); //Loads the codeigniter base instance (The object your controller is extended from. & for php4 compatibility
    $CI->load->database();
    $CI->load->library('session');

    if (!is_null($CI->session->userdata('user'))) {
    ...