【发布时间】:2016-10-09 02:47:31
【问题描述】:
我有一个名为 Product 的模型,它具有以下自引用多对多关系:
// parent products based on the product_product pivot table
public function parent_products()
{
return $this->belongsToMany('App\Product', 'product_product', 'child_id', 'parent_id')->withPivot('amount');
}
// child products based on the product_product pivot table
public function child_products()
{
return $this->belongsToMany('App\Product', 'product_product', 'parent_id', 'child_id')->withPivot('amount');
}
// movements
public function movements()
{
return $this->hasMany('App\Movement', 'product_id');
}
因此任何产品都可以“理想地”拥有无限的祖先和子代。
此外,任何产品都是其他产品的组合,就像“组件”一样。
因此,我需要根据每个孩子的可用性以及他们构建产品所需的数量来返还“可生产”产品的数量。
如果这是一个层次深度的东西,没有问题,但系统应该让你有许多深度层次,所以任何孩子也可以由孩子组成。
我无法控制每个产品的深度->产品->产品链。
除了性能问题,我需要一种递归方式来返回链中每个产品的“可生产”数量。
我设法像这样递归地让孩子们直到链的末端
public function child_products_recursive()
{
return $this->child_products()->with(['child_products_recursive','movements'=>function($query){
$query->whereDate('ready_date', '<=', date('Y-m-d'))->sum('amount');
}]);
}
它有效。
但无法弄清楚如何从中返回值或如何导航每个模型。
涉及的表是这样“简化”的:
products
---------
id | name
---------
1 | something
2 | something
3 | something
4 | something
5 | something
6 | something
7 | something
product_product
-----------------------------
parent_id | child_id | amount (amount of children in the parent product)
-----------------------------
1 | 2 | 2
1 | 3 | 4
2 | 4 | 1.5
2 | 5 | 3
4 | 6 | 0.7
4 | 7 | 0.3
movements table
-------------------------------------
id | product_id | date | amount
-------------------------------------
1 | 4 | 2016-01-06 | 300
2 | 5 | 2016-01-06 | 200
3 | 6 | 2016-01-06 | 125
4 | 7 | 2016-01-06 | 1000
所以产品 ID 1 没有动作,但可以基于没有动作但也可以由子产品构建的子产品构建为多个部分。
这是视觉表示(我之前没有按照表格的数据,只是为了给出想法):
Wine box Wine box
6 bottles 12 bottles ---1x-- Box for
of Wine3 of Wine4 12 Bottles
/ \ | \
/ \ | \
x6 x1 | x12
/ \ | \
Bottle Box for | Bottle
of Wine3 6 Bottles | Label
/ | \ |
/ | \ / \
/ | \ / \
/ | \ / \
x1 x0.75 x1 x12 x9
/ | \ / \
Empty | Bottle Wine4
Bottle XY | Cork ABC
|
|
|
|
Wine3
/ \
/ \
x0.7 x0.3
/ \
Wine1 Wine2
提前致谢。 咻
【问题讨论】:
-
是的,我想我会采用这种方法,但想知道是否有人遇到了类似的问题,并以比我的尝试更简洁、更简单的解决方案解决了
标签: php mysql laravel recursion laravel-5