CodeIgniter Forums
Include a model class without initialize it. - 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: Include a model class without initialize it. (/thread-33098.html)



Include a model class without initialize it. - El Forum - 08-16-2010

[eluser]megablue[/eluser]
I want to extend a model class but somehow $this->load->model() will initialize it as an object. Is there anyway to do it with CI or do I need to use include()?


Include a model class without initialize it. - El Forum - 08-16-2010

[eluser]megablue[/eluser]
I tried another way which i still run into a problem.

I tried to load the base model as usual.

$this->load->model('the_base_model', 'basename');

then I tried to unload the base model in order to load the extended model;

unset($this->basename);

//the_extended_model is extended from the_base_model
$this->load->model('the_extended_model', 'basename');

Yet the extended model is unable to initialize.

It can work if I give the 'basename' a name change.

So is that means CI keeps track of a list of declared object name somewhere and prevent it from redeclaring?


Include a model class without initialize it. - El Forum - 08-16-2010

[eluser]danmontgomery[/eluser]
Yes, CI keeps a list of instantiated object. You should be using the subclass_prefix setting in config/config.php, then loading the model normally (http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html).

If your subclass_prefix is MY_ (default), you would have MY_Model as your base class, then you would extend that class, and load it normally.

MY_Model.php
Code:
class MY_Model extends Model {
    // Some code
}

other_model.php
Code:
class Other_model extends MY_Model {
  // Some code
}

Controller
Code:
$this->load->model('other_model')



Include a model class without initialize it. - El Forum - 08-16-2010

[eluser]megablue[/eluser]
[quote author="noctrum" date="1281980046"]Yes, CI keeps a list of instantiated object. You should be using the subclass_prefix setting in config/config.php, then loading the model normally (http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html).

If your subclass_prefix is MY_ (default), you would have MY_Model as your base class, then you would extend that class, and load it normally.

MY_Model.php
Code:
class MY_Model extends Model {
    // Some code
}

other_model.php
Code:
class Other_model extends MY_Model {
  // Some code
}

Controller
Code:
$this->load->model('other_model')
[/quote]

Yes, i knew about extending the CI base model with CI way.
However i have a fairly dynamic application that requires multiple 'base models' base on different situations. Some models share fair amount of code even their default methods, some are distinctly different.