更改';后代';与';兄弟姐妹';关系


Change 'descendants' relationship to 'siblings' relationship

我有如下variants表:

+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| parent_product_id | int(10) unsigned | NO   | MUL | NULL                |                |
| child_product_id  | int(10) unsigned | NO   | MUL | NULL                |                |
+-------------------+------------------+------+-----+---------------------+----------------+

带约束:

CONSTRAINT `variant_products_child_product_id_foreign` FOREIGN KEY (`child_product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
CONSTRAINT `variant_products_parent_product_id_foreign` FOREIGN KEY (`parent_product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE

假设它充满了:

| id | parent_product_id | child_product_id | 
|----+-------------------+------------------| 
| 28 | 9                 | 11               | 
| 29 | 17                | 30               | 
| 30 | 9                 | 59               | 
| 31 | 9                 | 60               | 
| 32 | 17                | 25               | 

起初,业务需求是一个(父)产品可以有多个子产品。在我的Product型号中,我有

public function variants()
{
    return $this->hasMany('App'Variant::class, 'parent_product_id', 'id');
}

并且在Variant模型中:

public function child()
{
    return $this->belongsTo('App'Product::class, 'child_product_id');
}

当我使用:查询Product(id:9)时

$query->with([
    'variants.child'  => function ($query) {
        $query->select(['products.id', 'products.name'])
    },
]);

我得到了很好的回应:

{
  "id": 9,
  "name": "Foo",
  "description": "Ipsam minus provident cum accusantium id asperiores.",
  "variants": [
    {
      "id": 11,
      "name": "Bar"
    },
    {
      "id": 59,
      "name": "Fizz"
    },
    {
      "id": 60,
      "name": "Buzz"
    }
  ]
}

当询问产品59时,没有任何变体。

现在,我需要重新定义我的关系,这样产品和变体将成为兄弟姐妹而不是后代

例如,在询问产品59之后,期望的回答是:

{
  "id": 59,
  "name": "Fizz",
  "description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
  "variants": [
    {
      "id": 9,
      "name": "Foo"
    },
    {
      "id": 11,
      "name": "Bar"
    },
    {
      "id": 60,
      "name": "Buzz"
    }
  ]
}

如何在不改变数据库结构的情况下实现它。非常感谢您的帮助和提示。

编辑:两个注释:

  • 每个孩子只能有一个父母。(有一个唯一的钥匙数据库中的child_product_id列。)
  • "嵌套"只能有一个级别。这意味着,如果一个产品已经是一个孩子,它就不能是另一个的父母

正如我在评论中所说,我认为最好保留某种类型的父母,以更好地管理兄弟姐妹关系。例如,如果你想让59岁、9岁、11岁、60岁的孩子成为兄弟姐妹,你可以为他们所有人保留一个共同的父母id,比如999,这将使他们成为兄弟姐妹。

另一件事是,如果每个项目只有一个parent_product_id,则不需要将其保存在单独的表中。您可以将parent_product_id保存在同一个表products中,并在'App'Product中设置变体,如下所示:

public function variants()
{
    return $this->hasMany('App'Product::class, 'parent_product_id', 'parent_product_id');
}

现在,您可以通过对查询部分进行一点修改来获得兄弟姐妹的列表:

$query->with([
    'variants'  => function ($query) {
        $query->select(['products.id', 'products.name'])
    },
]);