0

please look at this.

The code below is from my model class (using datamapper orm)

 function login()
    {

        $u = new User();


        $u->where('username', $this->username)->get();


        $this->salt = $u->salt;

        $this->validate()->get();


        if (empty($this->id))
        {
            // Login failed, so set a custom error message
            $this->error_message('login', 'Username or password invalid');

            return FALSE;
        }
        else
        {
            // Login succeeded
            $data = array
            (
                'username' => $u->username,
                'usergroup' => $u->usergroup->get(),
                'is_logged_in' => true

            );
            $this->session->set_userdata($data);


            return TRUE;
        }
    }

when i do this i get

**Fatal error: Call to a member function set_userdata() on a non-object**

but when i do this instead

$data = array
            (
                'username' => $u->username,
                'usergroup' => $u->usergroup->get(),
                'is_logged_in' => true
            );
            $obj=& get_instance();
            $obj->session->set_userdata($data);

It works.

Please what is the right way to get this working ?

Thanks in advance.

4
  • Did you load the session library first? $this->load->library('session') Commented Nov 2, 2011 at 8:57
  • Uhm..and where are you using your first code? Commented Nov 2, 2011 at 10:39
  • Are you doing this from a library? In that case the $this will not contain the CI instance, see the section "Utilizing CodeIgniter Resources within Your Library" at codeigniter.com/user_guide/general/creating_libraries.html Commented Nov 2, 2011 at 10:48
  • sorry for my late response. I am doing all of this from a model (datamapper orm model) Commented Nov 2, 2011 at 14:13

2 Answers 2

1

your model did not extends CI_Model after that you have to add constructor to your model add this code to yours

function __construct()
    {
        parent::__construct();
        $this->load->library('session');
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Well, you didn't provide enough information. The first code looks fine, provided that:

  1. You actually load the session class before calling it (you also need to create an encryption key in your configs). $this->load->library('session'); $this->session->set_userdata($data);

  2. The above code, or your code, is inside a controller, a model or a view. $this relates to the CI's superclass, in particular to an instance of the Session class, so if you're calling that inside a helper (collection of functions), or inside a library (where you need to create a CI instance first), it won't work.

1 Comment

@mojotaker I still don't see where you load the session class...is it autoloaded?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.