在 CakePHP 中定义模型类的层次结构
默认情况下,CakePHP 有一个 AppModel 类,应用程序的每个模型都继承自它。在模型之间共享逻辑的常见设计模式是创建行为并将模型配置为 $actAs
该行为。
但是,如果我想引入这样的模型类层次结构怎么办?:
AppModel
|__ Vehicle
|__ Car
|__ Bike
|__ Helicopter
我尝试创建一个继承自 AppModel
的 Vehicle
类,然后每个子类都继承自车辆
。但 CakePHP 告诉我它找不到类 Vehicle
。
我怎样才能做到这一点以及我应该在 CakePHP 目录树中的什么位置创建 Vehicle
?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做应该不成问题
您只需要在声明之前确保 app::import() 是“父模型”。
或者你做了什么导致找不到模型?
如果我这样做,我会使用一个库作为我的“车辆”
它不一定是模型目录中的模型,
例如应用程序::导入('库','车辆');
Vehicle 扩展了 AppModel
Car 扩展了 Vehicle
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