Welcome Guest, Not a member yet? Register   Sign In
Extending my own model
#11

[eluser]msteudel[/eluser]
Buso you said you put it in your config.php file? At the end somewhere?
#12

[eluser]Buso[/eluser]
yes at the end. Didn't it work? I use this for both base models and controllers. You don't even need to use MY_Controller nor MY_Model inside of libraries if you do this. Use whichever prefix you want and put them inside /controllers or /models, as if they were regular controllers or models
#13

[eluser]Dan Horrigan[/eluser]
All these idea's for something that is simple:

In you autoload.php config file, add your user_model to the $autoload['model'] array.

That's it. CI will load the model, then the class exists and you can extend it.

Sometimes the simplest solutions are the best guys.

Dan
#14

[eluser]msteudel[/eluser]
Right, I did that as a workaround, but why autoload models in that don't need to be loaded in all the time?
#15

[eluser]Dan Horrigan[/eluser]
Then do
Code:
$this->load->model('user_model');

before you load the model hat extends it. The 2 other options are these:
1. Use an autoloader (as specified above...at the bottom of config.php would be early enough).
2. (NOT RECOMMENDED) Do an include of the "parent" model in the "child" model file (at the top).

Dan
#16

[eluser]Buso[/eluser]
Dan, doing $this->load is possible only for models and you have to remember doing it every time you need it for the first time in your app flow (problematic), and if you use autoload.php they will be loaded every time (inefficient)

__autoload magic function makes sure they are loaded only when needed, and you don't have to remember doing anything, nor using includes all over your files
#17

[eluser]Dan Horrigan[/eluser]
OK, if you want to use an autoloader, then at least do this:
Code:
function model_autoload($class)
{
    if(stripos('_model', $class) !== FALSE)
    {
        include APPPATH . 'models/' . $class . EXT;
    }
}
spl_autoload_register('model_autoload');

This way, it doesn't interfere with other autoloading (when CI gets it). It also makes sure it only loads models. Also, include_once is unnecessary, because the autoload method doesn't get called unless the clas doesn't exist (and include_once adds overhead).

Dan
#18

[eluser]Buso[/eluser]
Nice, thanks
#19

[eluser]lauripapchi[/eluser]
Yes i think its the better way to do it...
#20

[eluser]msteudel[/eluser]
Thanks guys.




Theme © iAndrew 2016 - Forum software by © MyBB