开发者

CakePHP how to use static values (categories)

I would like to have categories, and rankings for my content and users respectively, and I a开发者_开发技巧m not sure how to go about implementing this using CakePHP conventions?

Do I need to create a model?


That depends entirely on what these categories are supposed to do and not do. You could simply define a number of constants that you use for categorizing stuff. But, are categories...

  1. subject to change? Do you want to add more eventually?
  2. editable? May you want to change their names?
  3. nested?
  4. supposed to have more attributes than just their id? Names, descriptions?

If you answered Yes to any of the above, you'll want to store them as data in the database. This is independent of Cake, it's just sane data modeling. For Cake that means you'll need to create a model. The same goes for ratings.

So you'll have these tables:

  • users
    • hasMany ratings
  • categories
    • hasMany contents
  • contents
    • belongsTo categories
    • hasMany ratings
  • ratings
    • belongsTo users (polymorphic)
    • belongsTo contents (polymorphic)

You may want to separate user ratings and content ratings into two tables instead of using a combined polymorphic table (which just means that you have an extra column keeping track of whether a rating is for a user or for content).


i guess you are looking for something like this IF you dont want to use a model: http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/

one possible approach to use "enums" for things that maybe only have 1-5 states.

if you have more than 10 or you want to be able to dynamically modify them (label, active/inactive) you will need a separate table and model relation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜