Welcome Guest, Not a member yet? Register   Sign In
How to extend Model multiple times?
#1

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

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

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

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

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

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

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

[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?
#9

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

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




Theme © iAndrew 2016 - Forum software by © MyBB