4

By default CakePHP has a AppModel class and every model of an application inherits from it. A common design pattern to share logic between models is to create a behavior and configure a model to $actAs that behavior.

But what if I wanted to introduce a hierarchy of model classes like this?:

AppModel
  |__ Vehicle
        |__ Car
        |__ Bike
        |__ Helicopter

I have tried to create a Vehicle class that inherits from AppModel, and then every children class inherits from Vehicle. But CakePHP tells me it cannot find the class Vehicle.

How could I make this and where in the CakePHP directory tree should I create Vehicle?

Thanks!

elitalon
  • 9,191
  • 10
  • 50
  • 86
  • i like this idea so i search a little bit. There is a way to implement this, explained [here](http://bakery.cakephp.org/articles/eldonbite/2008/09/18/subclass-behavior) Haven't try it yet though... Other way will be to mess with the class_registry file or the app class that are in charge of the load... but i don't recomend it – api55 Sep 23 '11 at 19:38
  • I came across that behavior as well, but it seems that it tries to mimic the native behavior of class inheritance at application level. I think it's overkill – elitalon Sep 24 '11 at 06:56

1 Answers1

6

it should not be a problem to do so you only need to make sure you app::import() the "parent model" before you declare it. or what did you do that the model cannot be found?

If I do sth like that I use a lib to be my "vehicle" it does not have to be a model in the model directory

e.g. App::import('Lib', 'Vehicle');

Vehicle extends AppModel

Car extends Vehicle

mark
  • 21,691
  • 3
  • 49
  • 71
  • Correct, I believe; cake will not know automatically where to find the Vehicle class, without you tell it manually using import(). – Ivo Sep 24 '11 at 12:44