HABTM with additional columns for recipe ingredients

有些话、适合烂在心里 提交于 2019-12-23 04:21:27

问题


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 model 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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4032313/habtm-with-additional-columns-for-recipe-ingredients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!