Welcome Guest, Not a member yet? Register   Sign In
session library inside of models?
#1

[eluser]Philip Pryce[/eluser]
Hi, i'm kinda new to Code Igniter but its treating me well upto now and its definitely sped up my development time, theres one little problem i'm having however, when i try to run my user model, theres a session library i need to use to access some user information to check if they are logged in, i cant use the built in $_SESSION var so i'm using the session library, but it doesnt seem to load inside of models. I was just wondering if theres someway i could access the session library inside of my model that would be great.
Thanks in advance.
#2

[eluser]MadZad[/eluser]
Philip,
I autoload the session library via this line in my config/autoload.php:
Code:
$autoload['libraries'] = array('session');

so my models can just use the session library like so:
Code:
$uid = $this->session->userdata("user_id");

Good luck.
#3

[eluser]Jay Turley[/eluser]
[quote author="Philip Pryce" date="1206391772"]i'm using the session library, but it doesnt seem to load inside of models. I was just wondering if theres someway i could access the session library inside of my model that would be great.[/quote]

Is this really a problem? It could explain an issue I (as a newbie) am having with a current model I am working on.

I didn't see anything in the documentation about models not being allowed to load libraries. However, in the "Using CodeIgniter Libraries" section of the user guide, there is the sentence, "In most cases, to use one of these classes involves initializing it within a controller..."

I missed this until just now when I went looking for it.
#4

[eluser]Philip Pryce[/eluser]
Thank you for the reply, both of you. I'd just like to say, my Session library is already autoloaded, but for some reason it still cant use it.
For a bit more of a specific idea of whats going on, heres the error.
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: user_model::$session

Filename: models/user_model.php

Line Number: 84


Fatal error: Call to a member function userdata() on a non-object in /var/www/html/OpenNotes/system/application/models/user_model.php on line 84

And heres whats on line 84 and the two surrounding lines.
Code:
function authorize () {
    
        $user_id = $this->session->userdata('user_id'); //$_SESSION['OpenNote']['userinfo']['id'];
        $token   = $this->session->userdata('user_token'); //$_SESSION['OpenNote']['userinfo']['token'];
#5

[eluser]MadZad[/eluser]
Hm.
OK, code looks fine. Error message means $this->session is not resolving as expected in your model. Here's a couple of thoughts:

Try setting and retrieving the session 'user_id' in the controller that includes the model in question. Just making sure the session library itself is loading/working ok.

Can your model use other libraries? Perhaps try sending an email.

Does your model extend Model and start with a capital letter, as per user guide? That can be an easy one to miss
Code:
class Abc_model extends Model {

  function Abc_model() {
    parent::Model();
  }
...


If none of those sheds any light, I'm not sure where to try next. Even those are a shot in the dark.
#6

[eluser]Philip Pryce[/eluser]
wow thank you, i completely forgotten about that parent:Model(); i have no idea why i havent included it, i have on all my other models,
again thank you Smile




Theme © iAndrew 2016 - Forum software by © MyBB