Loading a Model inside a Model |
[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 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
[eluser]imn.codeartist[/eluser]
Here is the solution of your problem Code: class Big_model extends Model{ This will work
[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.
[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
[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]
[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.
[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.
[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 { |
Welcome Guest, Not a member yet? Register Sign In |