Welcome Guest, Not a member yet? Register   Sign In
Autoload model with usage of autoloaded libraries
#1

[eluser]Escee[/eluser]
Hi y'all,

I'm quite new to CodeIgniter, and I'm stuck, that's why I'm here ;-)

I managed to autoload models after tweaking the Loader.php file a bit, but the model that is autoloaded makes use of the session library which is autoloaded. When the model is autoloaded, the error occurs that the session library doesn't exist/isn't loaded.

So my question, is there a way that models can be autoloaded so they can make use of the autoloaded libraries such as the session library?

Thanks! :-)

Stefan
#2

[eluser]Derek Allard[/eluser]
Welcome to CI Escee. I hope after this gets resolved for you we see you around a lot more Wink

Can you post some relevant chunks of your model so we can see how you are using the session?
#3

[eluser]Escee[/eluser]
&uot;[quote author="Derek Allard" date=;1193695204"]Welcome to CI Escee. I hope after this gets resolved for you we see you around a lot more Wink

Can you post some relevant chunks of your model so we can see how you are using the session?[/quote]

Okay, for example, here is the constructor of the 'member' model which checks if a user is already logged in:

Code:
public function Member ( )
        {
            parent::Model ( );
            
            // If the session 'userid' exists get the username
            $userdata = $this -> session -> userdata ( 'userid' );
            if ( $userdata !== false && ctype_digit ( $userdata ) )
            {
                // Query for the username/id
                $this -> db -> select ( 'id, username' );
                $this -> db -> where ( 'id', $userdata );
                $userquery = $this -> db -> get ( 'users' );
                
                if ( $userquery -> num_rows ( ) == 1 )
                {
                    // Save query recieved data
                    $userdata = $userquery -> row ( );
                    $this -> userid = $userdata -> id;
                    $this -> username = $userdata -> username;
                    $this -> loggedin = true;
                }
            }
        }

And here's a bit of code for a new login:

Code:
public function ProcessLogin ( $username, $password )
        {
            // Get some data and check some stuff
            // ...

            // Login is ok
            $this -> username = $userdata -> username;
            $this -> userid = $userdata -> id;
            $this -> session -> set_userdata ( 'userid', $userdata -> id ); // Set de userid session
            $this -> loggedin = true;
            return true;
        }

When I first load the session library and then the member model in a controller, it works. But i'd like to autoload them both (the session library already does). But when I autoload them both, the member won't find/use/load/... the session library..

Hope you understand :-)

So far CodeIgniter rocks, great documentation, helps allot ;-)

Stefan
#4

[eluser]ELRafael[/eluser]
Remmember one thing Stefan:

If you autoload the model, it'll be avaible for all controllers. Even that controllers that won't use the model.

But i can't see an error, sorry.

I my case, i just autoload the session (and anothers) libraries. And i don't use $this->session inside models... i can be wrong, but i believe the function of a model is "talk" to database, not to talk another components.

I don't know the order of auto load, but the logic is load model then load session library, so session is avaible in the model, right?

Derek certainly can give you a better answer. And, for sure, a better writting than mine :-P
#5

[eluser]Derek Allard[/eluser]
Just a thought. Open up your system/libraries/Loader.php, and look for
Code:
// Autoload models
if (isset($autoload['model']))
{
    $this->model($autoload['model']);
}

try cutting that, and pasting it a few lines lower immediately after
Code:
// Load all other libraries
            foreach ($autoload['libraries'] as $item)
            {
                $this->library($item);
            }
        }

so that now it looks like
Code:
// Load all other libraries
            foreach ($autoload['libraries'] as $item)
            {
                $this->library($item);
            }
        }        

        // Autoload models
        if (isset($autoload['model']))
        {
            $this->model($autoload['model']);
        }

    }
    
    // --------------------------------------------------------------------

Any help?
#6

[eluser]Escee[/eluser]
I tried that before, then it didn't work. Strange enough it does work now, just reversed the loop for autoloading libraries with the loop for plugins, helpers etc. Big Grin

Thanks for helping me out Smile

@ELRafael:

[quote author="ELRafael" date="1193704278"]Remmember one thing Stefan:

If you autoload the model, it'll be avaible for all controllers. Even that controllers that won't use the model.

[..]

I my case, i just autoload the session (and anothers) libraries. And i don't use $this->session inside models... i can be wrong, but i believe the function of a model is "talk" to database, not to talk another components.

[..][/quote]

This sounds okay, but when you have users logged on to your site, how would you handle there sessions and stuff without a model?
#7

[eluser]Derek Allard[/eluser]
My pleasure, glad you got it running, and welcome onboard!
#8

[eluser]somewhatjaded[/eluser]
i had the same problem, and this fixed it as well (I was trying to autoload the EzAuth model).




Theme © iAndrew 2016 - Forum software by © MyBB