2015-12-21 28 views
5

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)使用:

我得到很好的迴應:

{ 
    "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列有唯一鍵。)
  • 只能有一個「嵌套」級別。這意味着,如果一個產品已經是一個孩子 - 它不能成爲另一個產品的父母。
+0

什麼如果產品59有自己的孩子?他們是否也會出現在最後的結果中。如果是這樣,查詢產品9時它們是否也必須出現?或者,如果9有自己的父母,那麼它會不會也顯示爲兄弟姐妹?我覺得這個定義的兄弟姐妹(包括父母也是這樣)是不明確的,如果一致應用會導致大量的「兄弟姐妹」。 – trincot

+0

每個孩子只能有一個家長。 (在數據庫的'child_product_id'列有一個唯一的鍵。)只能有一層嵌套。這意味着,如果一件產品已經是一個孩子 - 它不能成爲父母。 – user1292810

+0

我明白了。所以沒有孫子:-)。產品11是否也應該出現在您的結果中? – trincot

回答

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']) 
    }, 
]);