在 CakePHP 中管理多个模型的类别
我要创建一堆模型,这些模型将具有它们所属的不同类型的类别。例如,一个模型将 HABTM 一组类别,另一个模型将 HABTM 另一组。
我提出了一个想法 - 创建一个名为 Category 的模型,并拥有一个带有“model”字段的类别表,其中包含与该类别具有 HABTM 关系的模型的名称。然后其他模型会有这样的内容:
public $hasAndBelongsToMany = array(
'Category'=>array(
'conditions'=>array(
'Category.model'=>'Modelname'
)
)
)
这看起来不错,但是
- 这个代码将在每个模型中重复,这看起来很愚蠢,并且
- 某些类别将适用于多个模型,因此这些类别会有重复的数据库条目。
有更好的办法吗?感谢您的帮助!
I have a bunch of models that I'm going to be creating, and these models will have different types of categories that they can belong to. For example, one model will HABTM one set of categories, another model will HABTM another set.
I've come up with one idea - creating a model called Category, and having a categories table with a 'model' field, that contains the name of the model that this category has a HABTM relationship with. Then the other models would have something like this:
public $hasAndBelongsToMany = array(
'Category'=>array(
'conditions'=>array(
'Category.model'=>'Modelname'
)
)
)
This seems OK but
- this code will be repeated in every model which seems silly and
- some categories will apply to several models, so there would be duplicate database entries for those categories.
Is there a better way? Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用一个类别模型,并在类别模型中添加模型字段来指定相关模型。因此,您可以将不同的模型与一种类别模型一起使用...就像您所说的...
我认为这是一个好主意。
You can use one category model and add a model field to the category model to specify the related model. So you can use different models with one category model... Like you said...
I think this is a good idea.