Class Buyer {
String name
static constraints = {
}
}
Class Order {
String ref
static belongsTo = [buyer:Buyer]
static constraints = {
buyer(nullable:false)
}
}
在OrderController.groovy如何通過循環訪問數組或地圖來保存多行數據?
...
def someAction = {
//working
def data1 = ["buyer.id": 2, "ref": "xyz"]
def ord = new Order(data1);
ord.save();
def data2 = ["buyer.id": 2, "ref": "234xyz"]
def ord2 = new Order(data2);
ord2.save();
//But in a loop - its not working
def items = ['abc', 'def', 'ghi', 'jkl']
def data2 = [:]
for(e in items) {
data2 = ["buyer.id": 2, "ref" : e.value] //keeping buyer id same
def ord = new Order(data2);
ord.save();
data2 = [:] //just emptying it?
}
}
正如你會發現在「工作」上面,如果我能夠通過複製粘貼和definging新地圖,但如果我通過陣列嘗試循環保存多個行,它不起作用。任何想法如何通過循環訪問數組或地圖來保存數據?
有任何疑問,請與我們知道 感謝
謝謝你的答案。我曾嘗試.get()但不知何故,它沒有工作,我需要嘗試你的解決方案,我會讓你知道它是怎麼回事 – TigerTiger 2011-04-06 09:01:33
爲什麼您使用買方b =新買方(名稱:「foo」)而不是def b =新買方(名稱:「foo」)? – TigerTiger 2011-04-06 09:14:18
@Wbdvlpr:def b = new Buyer is ok。我不明白爲什麼Buyer.get(buyerId)失敗,它是否仍提供相同的結果或未能獲得買方? – 2011-04-06 09:53:51