Welcome Guest, Not a member yet? Register   Sign In
loading libraries and models from within a model
#1

[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()?
#2

[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.
#3

[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.
#4

[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.
#5

[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.
#6

[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.
#7

[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.




Theme © iAndrew 2016 - Forum software by © MyBB