2014-09-25 59 views
1

我需要給我的Angular JS應用程序提供一個JSON,它表示表格的父子關係。 母公司(集團):使用PlayFramework的JSON中的父母 - 孩子關係

+----+---------------+-------------+----------------+ 
| id | external_code | supplier_id | notes   | 
+----+---------------+-------------+----------------+ 
| 19 | asdfas  |   3 | sadfa   | 
| 23 | 454   |   1 | groupa1  | 
| 24 | sadfas221  |   2 | asfd   | 
| 25 | dsafas  |   2 | NULL   | 
| 21 | 4545   |   1 | asdfasf  | 
+----+---------------+-------------+----------------+ 

兒童(GroupItems):

+----------+---------+--------+ 
| group_id | item_id | status | 
+----------+---------+--------+ 
|  19 |  1 |  0 | 
|  19 |  2 |  0 | 
|  19 |  3 |  0 | 
|  25 |  2 |  0 | 
+----------+---------+--------+ 

我期望的JSON應該是這樣的:

[ 
{"groupId":"19", 
"notes":"sadfa", 
"extenalCode":"asdfas", 
"supplierId":"2", 
"itemCount":3 
"items":[{"itemId": "1","status":"Created", "weight":23}, 
     {"itemId": "2","status":"Created", "weight":23} 
     {"itemId": "3","status":"Created", "weight":23} 
     ] 

}, 

.... 

] 

的問題是如何插入和綁定子項父代表使用MySQL和PlayFramework2.0(Slick)的JSON語義?

+0

地圖嵌套文檔的順序爲文件數序列,每個嵌套levenl並添加父標識。然後使用insertAll來插入每個這些序列的頂級項目? – cvogt 2014-09-25 05:21:57

+0

@cvogt你能詳細解釋一下嗎?我對scala和流暢的API非常陌生。 – 2014-09-25 06:04:47

+0

誤讀你的問題,你不是在詢問數據庫插入,對吧? – cvogt 2014-10-02 23:49:49

回答

0

大致是這樣的:

val items = 
    GroupItems.join(Items).on(_.itemId === _.id).run // <- query fetching items with group_ids 
      .groupBy(_._1.groupId).toMap 
      .mapValues(_._2) // <- mapping Map values to only items 

// render groups to json and add a field items with the items (I may be wrong about Play's json api names) 
val json = Group.run.map(g => Json.toJson(g) ++ JsObject("items" -> Json.toJson(items(g.id))))