CodeIgniter Forums

Full Version: Loading a Model inside a Model
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Mat Marlow[/eluser]
Hi all, hope you can help, this is driving me crazy!
I've structured my code so that the controller loads a Model which then uses another model inside it, but I get an error when I try to call a method on the second model.

So, the controller 'Test' loads 'Big_model' upon which the method 'getAll()' is called which loads 'Little_model' and tries to call the method '$this->LittleModel->test()'.
The error I get is a PHP error: "Undefined property: Big_model::$LittleModel" which occurs only when I try to call the test() method.

Code:
// controllers/test.php

class Test extends Controller {
        
    function index()
    {
        $this->load->model("Big_model","BigModel");
    $this->BigModel->getAll();
    }
}

#===============================================================
// models/big_model.php

class Big_model extends Model{
    
    function getAll()
    {
        // load another model
        $this->load->model("Little_model","LittleModel");
        $this->LittleModel->test();
    }

}

#===============================================================
// models/little_model.php

class Little_model extends Model{
        
    function test(){
        die("HELLO WORLD!");    
    }
    
}


Is there something I'm missing? CI doesn't produce an error when I load Little_model, only when I try to use it. Please help!!!!!!

Thanks,
Mat

El Forum

[eluser]imn.codeartist[/eluser]
Here is the solution of your problem

Code:
class Big_model extends Model{
    
    function getAll()
    {
        // load another model
        $ci=&get;_instance();

        $ci->load->model("Little_model","LittleModel");
        $ci->LittleModel->test();
    }

}

This will work

El Forum

[eluser]wiredesignz[/eluser]
This is a bug in CodeIgniter: http://codeigniter.com/bug_tracker/bug/8941/

Using get_instance() inside a model should not be necessary.

El Forum

[eluser]Mat Marlow[/eluser]
Thanks guys, I really appreciate that. My code is now fixed.
Why do I always manage to find these bugs? I spend hours thinking I'm doing it wrong before posting on the forum, what a waste of time!

Thanks again,
Mat

El Forum

[eluser]alxjvr[/eluser]
thanks for this wiredesignz, i got mine to work by replacing line 185 with this:

$this->_ci_assign_to_models();

[quote author="wiredesignz" date="1255535094"]This is a bug in CodeIgniter: http://codeigniter.com/bug_tracker/bug/8941/

Using get_instance() inside a model should not be necessary.[/quote]

El Forum

[eluser]ardinotow[/eluser]
[quote author="alxjvr" date="1258638513"]thanks for this wiredesignz, i got mine to work by replacing line 185 with this:

$this->_ci_assign_to_models();

[quote author="wiredesignz" date="1255535094"]This is a bug in CodeIgniter: http://codeigniter.com/bug_tracker/bug/8941/

Using get_instance() inside a model should not be necessary.[/quote][/quote]

Thanks for reporting this bug wiredesignz. I've follow replacement from alxjvr, but still got problem when loading model inside _construct function. So, loading a model in place with the function that need to use other model is better option.

El Forum

[eluser]Keyur Shah[/eluser]
Thanks for the bug fix. It seemed strange how the models worked until you solved it. Hopefully it makes it into an official fix in the future.

El Forum

[eluser]franckmercado[/eluser]
great example!!

El Forum

[eluser]Ben Bowler[/eluser]
Is this bug still open? The bug trackers moved.

El Forum

[eluser]smick[/eluser]
Not sure about pre 2.0 but apparently in 2.0 you can just load models in the constructor like this:

Code:
class Some_model extends CI_Model {

    function __construct() {
        parent::__construct();
        
        //explicitly load any models used inside this model
        $this->load->model('another_model');
    }
    
    
}
Pages: 1 2