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

[eluser]msteudel[/eluser]
Hi All,

I would like to extend one of my models. (I know I could do a MY_model setup but I'm just interested in extending this one)

I have my models located in:

system/application/model

My model is

Code:
class user_model extends Model {
     // code ...
}

And I want to extend that:

e.g.

Code:
class student_model extend user_model {
     // code ...
}

But when I do that I get:
Code:
Fatal error: Class 'user_model' not found in /path/trunk/system/application/models/student_model.php

Is there a place I should add a include directive? I could autoload it in autoload file, but I don't want to always load that model all the time.

Any suggestions?

Thanks, Mark
#2

[eluser]seanloving[/eluser]
I think you have to capitalize the first letter of your model name
#3

[eluser]msteudel[/eluser]
I just tried that but that didn't work.
#4

[eluser]seanloving[/eluser]
Huh, I also get the same error when trying to...
... extend any model that extends MY_Model.
... extend any model that extends MX_Model (a la HMVC).
Anyone else explain this?
#5

[eluser]Buso[/eluser]
edit: Sorry sometimes I answer before I read -.-
#6

[eluser]echoDreamz[/eluser]
This is because CI will only load 1 class. I had this issue extending the controller class 2 levels deep.

CI_Controller --> MY_Controller --> Specialized_Controller

I had to use the PHP5 autoload function to including the MY_Controller class before trying to extend. You can also use PHPs include or require method to include the user_model file before you define the student_model class.
#7

[eluser]Buso[/eluser]
[quote author="echoDreamz" date="1273726735"]This is because CI will only load 1 class. I had this issue extending the controller class 2 levels deep.

CI_Controller --> MY_Controller --> Specialized_Controller

I had to use the PHP5 autoload function to including the MY_Controller class before trying to extend. You can also use PHPs include or require method to include the user_model file before you define the student_model class.[/quote]
Did you place the MY_Controller inside the libraries dir ? It's supposed to be loaded automatically if you do so. Then you can extend it.
__autoload will be needed if you want a 'deeper' base controller to be extended
#8

[eluser]echoDreamz[/eluser]
My mistake I explained it a bit wrong.

The issue came when I used the deepest controller, IE the welcome controller which inherited the clients specialized controller which inherited our in house controller which of coarse takes in the CI base controller.

When the welcome controller extended the specialized controller I got the error about it missing. So I used the __autoload to include the specialized controller. When I get my main PC up, ill post the code I used.
#9

[eluser]Buso[/eluser]
yep.. I use __autoload at the end of config.php. Not sure if that's the better place, but it works fine
#10

[eluser]Buso[/eluser]
[quote author="msteudel" date="1273629310"]
Any suggestions?

Thanks, Mark[/quote]

try placing this somewhere before codeigniter tries to load your model:

Code:
// AUTOLOAD BASE MODEL(S)
function __autoload($class) {
  if($class==='Some_Model' || $class==='Another_Model') {
    include_once( APPPATH . 'models/' . $class . EXT );
  }
}

you could use less strict condition, but since your base models shouldn't be too many, you can just write their full names




Theme © iAndrew 2016 - Forum software by © MyBB