CodeIgniter Forums
How to extend Model multiple times? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to extend Model multiple times? (/showthread.php?tid=22623)

Pages: 1 2


How to extend Model multiple times? - El Forum - 09-15-2009

[eluser]Burak Guzel[/eluser]
How do I do this?

Model_Foo extends Model
Model_Bar extends Model

Model_Foochild extends Model_Foo
Model_Barchild extends Model_Bar

From what I understand, I have to create a MY_Model class to extend Model. But I would like to have multiple base model classes that I can extend.

What's the best way to do this?

Thanks


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]bigtony[/eluser]
You don't have to create a MY_Model - only neede if you want to have some extra functionality that the default Model doesn't provide. But you can also create one called something else. I have one called "Base_model" (which extends Model) in the normal models directory. Then my other models just extend Base_model. So if you wanted, you could also have Base_model2 for other models.


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]BrianDHall[/eluser]
Actually, what you ask is actually how you do it. Extend the CI model class with Foo and one with Bar, then extend Foo and Bar however you want.

As far as I am aware there is no reason this wouldn't work just fine.


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]Burak Guzel[/eluser]
My problem was that I was getting class not found errors. I tried putting them in the library and autoloading, but then I got 'Model' class not found error. Then I put them back in the models folder and autoloaded them again, but it always creates those objects and calls the constructors, which I didn't want.

Then I found out just today that some people were using include_once() statement to include a class they want to extend from another class. I guess it makes sense that way. Although having to use include_once() felt a bit inconsistent with how CI works in general.


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]Carlos G[/eluser]
actually using include_once() has been the only way that i have can use my own clases as models and controllers.

i also think that should be a better and more Codeigniter-way to do this but it's not in the documentation.

if anybody has a good technique for this, please share it


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]Burak Guzel[/eluser]
PHP5 has __autoload() that does something similar. I'm not sure if it's possible to put that functionality in CI with the PHP4 compatibility requirements and all.

http://us2.php.net/manual/en/language.oop5.autoload.php

Having to individually include class files should become something of the past.


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]Burak Guzel[/eluser]
Well I just wrote my own solution. I put this at the end of the config.php file. (If there is a more proper way to inject code into CI, please let me know)

Code:
function __autoload($class) {
    if (file_exists(APPPATH."models/".strtolower($class).EXT)) {
        include_once(APPPATH."models/".strtolower($class).EXT);
    } else if (file_exists(APPPATH."controllers/".strtolower($class).EXT)) {
        include_once(APPPATH."controllers/".strtolower($class).EXT);
    }
}

Now you don't have to call include_once every time. But this works only on PHP5.


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]jedd[/eluser]
I've never really understood the appeal of multiple extensions to the core controllers.

OTOH I'm not really an OO kind of guy, but then neither are CI's Models - at least, not that I've seen anyone use them as such. Only recently did I see any use for MY_Model in fact, but even my usage of that is quite basic.

What kind of stuff do you need in every instance of your model, and that you can't squeeze into MY_Model and/or keep in a library?


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]Burak Guzel[/eluser]
I'm rewriting an old project. All data has to be imported into the new one. I use Models to represent the data.

So I'm creating a base model for the old projects data, and a base model for the new project. The old one will use a different db connection for example, because the data resides on a different server.


How to extend Model multiple times? - El Forum - 09-16-2009

[eluser]BrianDHall[/eluser]
I think your code is actually quite excellent - it is so intuitive that I really thought that was how CI worked to begin with - that is, that it would automatically try to get the definition for the class being extended. I guess this is a limitation with PHP4 compatability, but I don't know OOP well enough to say.