开发者

Managing categories for several models in CakePHP

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

  1. this code will be repeated in every model which seems silly and
  2. 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜