将'后代'关系改为'兄弟姐妹'的关系

时间:2015-12-21 14:07:49

标签: php laravel eloquent relational-database

我有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列。)
  • 只能有一个级别的“嵌套”。这意味着,如果一个产品已经是孩子 - 它不能成为另一个产品的父母。

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,我认为保留某种类型的父母以更好地管理兄弟关系仍然更好。例如,如果你想让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'])
    },
]);