Welcome Guest, Not a member yet? Register   Sign In
Loading models within models
#1

[eluser]bhogg[/eluser]
Am having an odd problem and hoping someone can figure out why it might be happening!

On an old codebase just upgraded to 1.7.2 (still using old validation, etc), I've got a couple models in the appropriate locations (application/models/opportunities_model.php, application/models/students_model.php), which I've modified from their original code in order to try and rule out interference in debugging the issue:

Code:
class Opportunities_model extends Model {

    function Opportunities_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
...

    function get($id)
    {
        echo "<p>getting session from opportunities_model...</p>";
        $this->load->model("sessions_model");
        echo print_r($this->sessions_model->get(9));
        return;

        ...
    }

    ...
}

class Students_model extends Model {
    
    function Students_model()
    {
        // Call the Model constructor
        parent::Model();
    }    

    ...

    function get($id)
    {
        echo "<p>getting session from students_model...</p>";
        $this->load->model("sessions_model");
        echo print_r($this->sessions_model->get(9));
        return;
    }
}

Then, in a controller:


Code:
...

    function edit($opportunities_id)
    {
        $this->load->library('validation');
        $this->load->model('opportunities_model');
        $this->load->model('students_model');
        
        $this->students_model->get("0251117");
        
        $this->opportunities_model->get($opportunities_id);
        ...
    }

Visiting myurl.com/opportunities/edit/699 shows:
Quote:getting session from students_model...
Array ( [0] => Array ( [id] => 9 [name] => September 2010 Selections [hide] => 0 [current] => 1 [allowedit] => 0 [allow_contact_view] => 1 [selection_est] => 2010-09-27 18:00:00 [created_est] => 0000-00-00 00:00:00 [obslt_est] => ) ) 1

getting session from opportunities_model...
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Opportunities_model::$sessions_model

Filename: models/opportunities_model.php

Line Number: 493

Fatal error: Call to a member function get() on a non-object in /private/var/www/community/system/application/models/opportunities_model.php on line 493

Why on earth would one model be able to load another unrelated model from within and not the other?

Thanks for any insights!
#2

[eluser]InsiteFX[/eluser]
try autoloading the sessions_model in application/config/autoload.php

InsiteFX
#3

[eluser]bhogg[/eluser]
Awesome, looks like:

Code:
$autoload['model'] = array('sessions_model');

did the trick. Any ideas as to why the manual load would not work? Will try and debug this a bit more and see what's going on under the hood.
#4

[eluser]bhogg[/eluser]
With some debugging it's definitely hitting line 184 of Loader.php, and being added to the CI instance->sessions_model, but is not visible from $this in the model when it tries to access it. Loading it ahead of time does the trick at the cost of having to auto load nearly everything.

If I find anything else out will post here or will just upgrade to CI 2.0 which may have this resolved Smile
#5

[eluser]danmontgomery[/eluser]
You can also just:

Code:
$CI =& get_instance();
$CI->load->model('my_model');

$CI->my_model->func();
// or
$this->my_model = $CI->my_model;
$this->my_model->func();

I would avoid autoloading everything if at all possible.
#6

[eluser]bhogg[/eluser]
Thanks for the note - yes, could definitely manually assign the model instance to the current model's $this, it's just strange that it's not propagating down on it's own unless it's not meant to. From the Loader function it seems to add it to just the CI instance rather than each already loaded model, and the other model that is working already has that model in it's $this, so this issue may just be by design. I'm considering revamping things in the codebase to use DataMapper anyway so this may become a non-issue shortly!




Theme © iAndrew 2016 - Forum software by © MyBB