在单独的桌子上自我参考 - 在Laravel的BoM

时间:2015-05-14 19:30:11

标签: laravel laravel-4 laravel-5 self-reference self-referencing-table

我很好奇是否有一种使用eloquent实现物料清单(程序集)类型递归系统的简单方法?以下是我正在使用的两个表格结构:

inventory表:

+----+------------+-------------+ | id | name | is_assembly | +----+------------+-------------+ | 1 | Table | 1 | +----+------------+-------------+ | 2 | Table Top | 0 | +----+------------+-------------+ | 3 | Table Legs | 0 | +----+------------+-------------+

inventory_assemblies表:

+----+--------------+---------+----------+ | id | inventory_id | part_id | quantity | +----+--------------+---------+----------+ | 1 | 1 | 1 | 1 | +----+--------------+---------+----------+ | 2 | 1 | 2 | 1 | +----+--------------+---------+----------+ | 3 | 1 | 3 | 4 | +----+--------------+---------+----------+

此汇编表应表示“1”表包含1个桌面和4个桌腿。

广告资源模型:

class Inventory extends Eloquent
{
    public function assemblies()
    {
        return $this->hasMany('InventoryAssembly', 'inventory_id', 'id');
    }

    /**
     * Returns all of the assemblies items recursively.
     *
     * @param bool $recursive
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getAssemblyItems($recursive = true)
    {
        /*
         * Grab all of the current item's assemblies not including itself
         */
        $assemblies = $this->assemblies()->where('part_id', '!=', $this->id)->get();

        $items = new Collection();

        // We'll go through each assembly
        foreach ($assemblies as $assembly)
        {
            // Get the assembly part
            $part = $assembly->part;

            if ($part)
            {
                // Dynamically set the quantity attribute on the item
                $part->quantity = $assembly->quantity;

                // Dynamically set the assembly ID attribute to the item
                $part->assembly_id = $assembly->id;

                // If recursive is true, we'll go through each assembly level
                if($recursive)
                {
                    if($part->is_assembly)
                    {
                        /*
                         * The part is an assembly, we'll create a new
                         * collection and store the part in it's own array key,
                         * as well as the assembly.
                         */
                        $nestedCollection = new Collection([
                            'part' => $part,
                            'assembly' => $part->getAssemblyItems(),
                        ]);

                        $items->add($nestedCollection);

                    } else
                    {
                        // The part isn't an assembly, we'll just add it to the list
                        $items->add($part);
                    }
                } else
                {
                    /*
                     * Looks like the dev only wants one level
                     * of items, we'll just add the part to the list
                     */
                    $items->add($part);
                }
            }
        }

        return $items;
    }
}

InventoryAssembly模型:

class InventoryAssembly extends BaseModel
{
    public function item()
    {
        return $this->belongsTo('Inventory', 'inventory_id', 'id');
    }

    public function part()
    {
        return $this->belongsTo('Inventory', 'part_id', 'id');
    }
}

现在这样可行,但是如果我将一个项目添加到它自己的程序集中,它将获得无限循环。所以这是我的问题:

  • 如何防止无限递归查询?
  • 我这样做了吗?
  • 有更简单的方法吗?
  • BoM模型是否更适合嵌套集设计?

我真的无法理解自引用的重复查询。我非常感谢任何的帮助,在此先感谢!!

编辑:在user3158900建议的库存模型本身上使用属于多种关系,我能够执行递归式汇编查询,如下所示:

库存模型(从以下答案修改):

class Inventory extends Eloquent {

    public function assemblies()
    {
        return $this->belongsToMany('Inventory', 'inventory_assemblies', 'inventory_id', 'part_id')
            ->withPivot(['quantity']);
    }

    public function assembliesRecursive()
    {
        return $this->assemblies()->with('assembliesRecursive');
    }

}

检索单个程序集:

$item = Inventory::with('assemblies')->find(1);

$items = $item->assemblies;

检索完整的递归程序集结果:

$item = Inventory::with('assembliesRecursive')->find(1);

$items = $item->assembliesRecursive;

$nestedItems = $items->get(0)->assemblies;

$nestedNestedItems = $items->get(0)->assemblies->get(0)->assemblies;

1 个答案:

答案 0 :(得分:1)

这变得容易多了。这可能看起来不像它,但它实际上属于很多,其中Inventory属于许多自身,inventory_assembly是一个数据透视表,实际上甚至不需要模型与它一起使用。

这是库存模型

class Inventory extends Eloquent {

    public function assemblies()
    {
        return $this->belongsToMany('Inventory', 'inventory_assemblies', 'inventory_id', 'part_id')
            ->withPivot(['quantity']);
    }

}

以下是我如何获得某个库存物品的装配集合。

$item = Inventory::with('assemblies')->find(1);

$assemblies = $item->assemblies;

编辑:刚刚意识到您使用的是Laravel 4.删除了名称空间。

另一个编辑:我不认为这已经解决了。如果例如支腿是需要支腿和硬件的组件,则硬件是需要不同螺母/螺栓/工具组的组件,如果工具组是需要扳手a和螺丝刀b等的组件......这种方法只会让你像腿一样深,并会忽略其他一切。

在这种情况下,我们会解决所有错误并忽略我所说的一切。这就是所谓的嵌套集模型。您可以在http://en.wikipedia.org/wiki/Nested_set_model

了解更多相关信息

在这种情况下,还有一个Laravel包应该为您处理这种关系。从我的头撞墙上拯救了我几次。 https://github.com/etrepat/baum#node-relations

对于Eloquent中的嵌套关​​系,这也是可能的,您将不再需要assemblyRecursive函数。你可以尽可能深入。

$item = Inventory::with('assemblies.assemblies.assemblies')->find(1);

foreach($item->assemblies as $assembly) {
    if($assembly->is_assembly) {
        // Do things for parent items
        foreach($assembly->assemblies as $nested_assembly_a) {
            // Do things for 1 deep nests

            if($nested_assembly_a->is_assembly) {
                foreach($nested_assembly_a->assemblies as $nested_assembly_b) {
                    // Do things for 2 deep nests

                }
            } else {
                // Non-assembly 1 deep child
            }
        }
    } else {
        // Non-Assembly parent
    }
}