• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
issue loading model in library

#1
[eluser]gscharlemann[/eluser]
I've added a new library to my application, but I'm getting a call to member function on non-object error:
Call to a member function get_saved_races() on a non-object

Here's the new library:
Code:
class Races
{

    function Races()
    {
        $this->ci =& get_instance();
    }
    
    function get_saved_races($user_id) {
        $this->ci->load->model('race_model', 'race');
        return $this->race->get_saved_races($user_id);
    }
}
I've tried this as well with no luck:
Code:
function get_saved_races($user_id) {
        $this->ci->load->model('race_model');
        return $this->race_model->get_saved_races($user_id);
    }

I've been playing with CI long enough I thought I could figure it out, but after multiple searches and over an hour of being stuck I'm getting frustrated. Thanks for any insight.

#2
[eluser]BrianDHall[/eluser]
Code:
class Races
{

    function Races()
    {
        $this->ci =& get_instance();
    }
    
    function get_saved_races($user_id) {
        if (! isset($this->ci->race))
        {
             $this->ci->load->model('race_model', 'race');
        }
        return $this->ci->race->get_saved_races($user_id);
    }
}

That ought to do it I believe.

The new if() just prevents multiple calls to load if it's already loaded, and since you are using $this->ci->load then the model being loaded should be located in the CI super object.

EDIT: Actually, this isn't right - it is an infinate loop. The get_saved_races function calls itself, which calls itself...

You need to fix the return value so it isn't infinately recursive, but this should answer the problems you had.

#3
[eluser]gscharlemann[/eluser]
That did it. Thanks for the help and quick response!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.