【发布时间】:2016-04-13 12:15:02
【问题描述】:
我发了一篇关于食谱和成分的帖子,但不幸的是我的帖子不够清楚,所以我试着在这篇文章中更清楚。
我有两个 SQL 表:
PRODUCTS (id, code, name)
RECIPES (product_id, ingredient_id, qty)
product_id 和 Ingredient_id 都是 PRODUCTS 表的 id。 “最终”产品可以由子产品复合。
我想在一个视图中显示制作食谱所需的所有食谱,并带有树形视图。
我会做一个例子,为此,我们可以有一个这样的数据库提取:
PRODUCTS (id is auto_increment)
code | name |
________|________________|
A | Product A |
A1 | Product A1 |
A2 | Product A2 |
A21 | Product A21 |
A22 | Product A22 |
A3 | Product A3 |
RECIPES
product_id | ingredient_id | qty |
_____________|_______________|_____|
A | A1 | 5 |
A | A2 | 9 |
A | A3 | 11 |
A2 | A21 | 2 |
A2 | A22 | 3 |
例如,要制作 10 个产品 A,我想显示这个:
Product A - 10
--> Product A1 - 50 (5*10)
--> Product A2 - 90 (9*10)
------> Product A21 - 180 (2*(9*10))
------> Product A22 - 270 (3*(9*10))
--> Product A3 - 110 (11*10)
我只能显示成分列表(A1、A2、A3),但不能显示产品 A2 的所有子配方,例如 A21 和 A22。
我在我的控制器中这样做:
$ingredients[] = [$codeProduct=> $this->Recipes->getListOfIngredients($codeProduct)];
getListOfIngredients 函数:
public function getListOfIngredients($code = null){
if ($code <> null){
$ingredients = $recipes->find('all', ['conditions' => ['Recipes.product_id = ' => $code]]);
return $ingredients->toArray();
}
}
使用该代码,$ingredients[] 包含产品的所有成分,但我如何添加所有子配方?
感谢您的帮助。
【问题讨论】:
-
你最好给我们看一些代码,否则这个问题会被否决并关闭
-
@RiggsFolly 我编辑了我的帖子。