views:

29

answers:

1

I'm working on a model that tracks user data and stores it in a session, where appropriate. Here's the basic structure of it:

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;               }
    }
}

The issue I'm having is right in the constructor. Whenever it hits the seventh line (checking if the user key is null) it errors out, saying:

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

Any ideas on why?

+1  A: 

My guess is you're trying to load a library from within a model which is not directly possible. Try instead:

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'))) {
    ...
Maurice Kherlakian
Wow... I had no idea that you couldn't do that...
Nate Wagar