Welcome Guest, Not a member yet? Register   Sign In
Loading a model within a model.
#11

[eluser]gtech[/eluser]
Quote:That is the behavior I would like, however it is not working. And I’d like to avoid rewriting code.

This is very odd! as I tested the above code and cut and pasted it directly (even the result was cut and pasted from the browser), it worked fine on my installation, honest! can anyone else get it to work?
#12

[eluser]mdg5w[/eluser]
I read another post, and it that did a similar thing and it works fine now. Don't know what I had wrong before.

Thanks
#13

[eluser]MPress[/eluser]
gtech, you're right about it only working if it is loaded in the model's constructor. I was wrong on the point. However, if you want to be able to load a secondary model outside of the constructor, something like the above load_model() method that I whipped it, placed in a base model, will be necessary. This is at least true on my installation.
#14

[eluser]gtech[/eluser]
Hi MPress,

Thanks for the info! I have bookmarked this discussion and I will use your method when I want to be able to load a model within a function (so the model is only loaded when needed).
#15

[eluser]MPress[/eluser]
The problem is in the 'model' method of Loader.php.

You just have to add
Code:
$this->_ci_assign_to_models();
to the last line (after line 169) of the 'model' method and the problem disappears. Without this line, the reference to the newly instantiated model is not being assigned to the subject (calling) model and only exists in the CI instance.

I'll reference this thread in a bug report and hopefully this will be remedied in the next release.
#16

[eluser]gtech[/eluser]
I have tried that out and it works, thank you!

I put your fix in, moved the load in my testmod.php above from the constructor to the test() function and it works a treat!
#17

[eluser]MPress[/eluser]
Here's a temporary fix until this issue is resolved. Put this in a base controller:
Code:
nevermind

*edit*

hmmm, only seems to work if $this->model('arg') is called after a $this->load->model('arg'). Something weird is going on.

Will look into..

Okay, I see now. The Loader class is handled differently under PHP 4 and PHP 5...

OKAY. If you want you can do something like this:

Code:
class ApplicationLoader extends CI_Loader{

    function ApplicationLoader(){

        parent::CI_Loader();
    }

    function model($model, $name = '', $db_conn = FALSE){

        parent::model($model, $name, $db_conn);
        parent::_ci_assign_to_models();
    }
}
Or don't. I really don't care anymore.
#18

[eluser]gtech[/eluser]
lol, are you alright Mpress?
#19

[eluser]CodeOfficer[/eluser]
Really I don't understand why it would be so complicated, the code below as I said before works fine. After a $this->CI =& get_instance(); you can load them wherever you want, in the constructor or in a method. The loader only returns a reference to a model if its already been loaded before, its not not a waste of resources in the least.

I would never want to load a model in a constructor if only 2 of 12 methods in my class used that model. its silly.

Code:
class MyModel extends Model
{
    private $CI;
    
    function MyModel()
    {
        parent::Model();
        $this->CI =& get_instance();    
    }
    
    function whatever()
    {
        $this->CI->load->model('model1');    
        $this->CI->model1->method1();
    }
    
    function woot()
    {
        $this->CI->load->model('model2');    
        $this->CI->model2->method2();    
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB