Welcome Guest, Not a member yet? Register   Sign In
Model initialization
#1

[eluser]liri[/eluser]
Hey,


When loading up a model from a controller such as:
Code:
$this->load->model('model_name');
why isn't the model's constructor called? or rather, what exactly happens when that 'loading' happens?
#2

[eluser]InsiteFX[/eluser]
The constuctor is called!

If you have a model the constructor will be called it
sets parent to model.

Whatever you put in the constuctor will be carried out.
You can also pass in parameters to the constuctor.

InsiteFX
#3

[eluser]überfuzz[/eluser]
I guess you're looking for first aid here. ;-)
In controller
Code:
$this->load->model('uber_model');
$this->uber_model->make_a_cup_of_coffee($param);


In model:
Code:
function make_a_cup_of_coffee($param)
{
   echo "Coffee is coming up!";
}
#4

[eluser]liri[/eluser]
Uhmm, nope.
I have:
Code:
class Menu extends Model
{
    
    public function __construct()
    {
        
        var_dump($this->modules_m->get_modules());
        
    }
and that doesn't seem to work when I load up the model.
Actually, more interesting is that now it errors on the call to the $this->modules_m->get_modules()
even though this did work just a few minutes ago... odd Smile

A model can use the $this-> and call already loaded other models, right?
#5

[eluser]WanWizard[/eluser]
Euhrm, not quite.

I'm missing a
Code:
parent::Model();
in your constructor.

The parent constructor will take care of the creation of local class variables for every library loaded, so you can use $this->library. Note that this is only for loaded libraries, not for models!

It also doesn't work bidirectional, so if you load A, then B, you can access A from B, but not B from A, as B wasn't loaded when A was instantiated.
#6

[eluser]liri[/eluser]
You're right, I totally left out the parent constructor thanks.




Theme © iAndrew 2016 - Forum software by © MyBB