Define a hierarchy of model classes in CakePHP
-
26-01-2021 - |
题
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!
解决方案
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