开发者

HABTM with additional columns for recipe ingredients

For my recipe-sharing website, I want to create a database and CakePHP models for the recipes and their ingredients.

I created two tables: recipes and ingredients. The third table is a HABTM table for ingredients_recipes, which also stores the amount of ingredients needed for the recipes. How do I create a mod开发者_运维技巧el for the third table?

The third table would look something like this:

recipe_id

ingredient_id

amount

measurement_unit

I also thought of adding another table for measurement_units to store all possible units (ex. tablespoon, tea spoon, cup, etc.). Would that be too much?


HABTM, within Cake's ORM, is actually an abstraction of the following two model association structures (using your example):

Recipe
-> hasMany IngredientsRecipe
           -> belongsTo Ingredient

and

Ingredient
-> hasMany IngredientsRecipe
           -> belongsTo Recipe

The IngredientsRecipe model is inferred, and used only to link the two first-class models, Ingredient and Recipe.

However, in your case, you actually want IngredientsRecipe to be a first-class model, which breaks the HABTM abstraction. In fact, what you need to do is explicitly define the two association structures above, so that Cake treats the IngredientsRecipe model as a first-class citizen, allowing you to query against it, save records to it, etc.

Also, no, I don't think it's too much to create an additional MeasurementUnit model. It will provide you much more flexibility down the line.


Think of it like this then treat it one chunk at a time:

`Recipe` (1)--(n) IngredientsRecipe (n)--(1) Ingredient
  1. In Recipe, create the association to IngredientsRecipe
  2. In Ingredient, create the association to IngredientsRecipe
  3. In IngredientsRecipe create the association to Recipe
  4. Still in IngredientsRecipe create the association to Ingredient

While you're doing it, forget about HABTM and think instead about hasMany and belongsTo

Incidentally, you are not bound to call the model IngredientsRecipe, that is just the default/convention.

When you're done, treat it like hasMany, belongsTo or HABTM as appropriate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜