Cakephp/SQL how to define a hasandbelongstomany relationship with quantities?
My problem is as follows. I have the following models: Recipe and Ingredient
. I want to define the two in a HasAndBelongsToMany
relationship, but the complication comes from the following: I want recipes to be able to possess a quantity of each ingredient.
Quantity will be defined by two fields: Amount and Unit. So on an abstract 开发者_JAVA百科level here's an example of what I want to see/be able to query:
Apple Pie:
Ingredients:
1 pound of apples
2 tablespoons of sugar
Orange Juice:
Ingredients:
1 cup of water
0.5 tablespoons of sugar
5 gobbles of googber
And I also wish to be able to query an ingredient and have it return which recipes are tied to it:
Recipes that contain sugar: Apple Pie and Orange Juice Recipes that contain googber: Orange Juice
I know this is strictly speaking a sql schema question, but since I am implementing the website in cakephp, I would prefer if the answers references the Model/Controller/View paradigm that cakephp uses as well as the builtin cakephp methods.
(Just so it would be easier for me to convert your solutions to my code. I do not like digging in the cakephp documentation, their search function is very buggy)
http://book.cakephp.org/view/1650/hasMany-through-The-Join-Model
If you are not, you should be using debug() at all times when trying to resolve your data arrays. You can use debug in your views, controllers, models etc. It is VERY useful.
Without seeing your data array structure, I would first say try to access the User model from a find like:
$this->Model_1->Model_2->Model_3->find('all'); or you can set your recursion level to 2 and see how much data you get back and all relations in an array. But use debug() to find out if you need to modify your model relations etc..
精彩评论