CodeIgniter Forums
loading libraries and models from within a model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: loading libraries and models from within a model (/thread-17041.html)



loading libraries and models from within a model - El Forum - 03-24-2009

[eluser]GSV Sleeper Service[/eluser]
What's the correct way to load libraries and (other) models from within a model?
The user guide is pretty vague about this.

a confusing example below
Code:
class Quotes_model extends Model {

    ...

    function save_quote()
    {
        $this->load->model('address_model'); //no error raised
        $this->address_model->get_primary_address(); //Fatal error: Call to a member function get_primary_address() on a non-object
        echo $this->session->userdata('example_session_var'); //works fine
        
        // CI get instance method below
        $CI =& get_instance();
        $CI->load->model('address_model');
        $CI->address_model->get_primary_address(); //this works fine
    }

}
so, why does '$this' in a model sometimes refer to the CI superclass (sessions, input etc), but in other cases I have to use get_instance()?


loading libraries and models from within a model - El Forum - 03-24-2009

[eluser]Stelian Mocanita[/eluser]
OOP wise models are just for database iterations so the best place to have them loaded would be the controller.

getinstance() would reiterate your $this superobject and use far more resources than needed which would make this not a very good practice. Use getinstance() where you have no way out but keep in mind it uses quite some resources.

Another work around would be sending the objects by reference to the model, but still not a good practice so I mostly use the #1 one.


loading libraries and models from within a model - El Forum - 03-24-2009

[eluser]xwero[/eluser]
I wouldn't load a model/library in another model. If you need a method of another model load that model and pass the result of the method as a parameter this will loosen the coupling between the models.


loading libraries and models from within a model - El Forum - 03-24-2009

[eluser]GSV Sleeper Service[/eluser]
[quote author="Stelian Mocanita" date="1237906551"]OOP wise models are just for database iterations so the best place to have them loaded would be the controller.[/quote]

hmmmm, in this specific case the 'address' model has nothing to do with the controller, it is only used by the 'quotes' model, so it doesn't really make sense to load the address model in the controller.


loading libraries and models from within a model - El Forum - 03-24-2009

[eluser]GSV Sleeper Service[/eluser]
[quote author="xwero" date="1237906829"]I wouldn't load a model/library in another model. If you need a method of another model load that model and pass the result of the method as a parameter this will loosen the coupling between the models.[/quote]

yeah, this makes sense. I'm not happy with the current set up.


loading libraries and models from within a model - El Forum - 03-24-2009

[eluser]Stelian Mocanita[/eluser]
Well since CI uses a SuperObject to load mostly everything it makes sense to have it loaded in the Controller since the methods would be in the $this object and accessible for requests.


loading libraries and models from within a model - El Forum - 04-14-2009

[eluser]rayray[/eluser]
It's easy to load models inside of models similar to loading them in a controller, but once loaded you must instantiate them (unless all you want to access is static members/methods). You can use the following from inside a model:

Code:
$this->load->model('SomeObjectModel');
$object = new SomeObjectModel();

You get this functionality because when the controller loads a model, it also assigns references to all it's currently instantiated objects. In the example above, you're using an instance of the Loader class which the is referenced in the controller with
Code:
$this->load
.

Given a non-trival schema, I can see many places how you'd want to access an model object inside another model.