gpt4 book ai didi

data-binding - Grails 数据绑定(bind)疑问

转载 作者:行者123 更新时间:2023-12-02 15:14:29 25 4
gpt4 key购买 nike

   Class Carro {
String name
String marca
String matricula

}

Class CarroMovel{

String pro1
String prop2
String prop3

Carro carro

static hasMany = [ carros: Carro]

}
 def save2 = {

def carroInstance = new Carro()
def carroMovelInstance = new CarroMovel()

carroInstance.name = params.name
carroInstance.marca = params.marca
carroInstance.matricula = params.matricula

carroMovelInstance.prop1 = params.carroMovel.prop1
carroMovelInstance.prop2 = params.carroMovel.prop2
carroMovelInstance.prop3 = params.carroMovel.prop3

carroInstance.save()
carroMovelInstance.carro = carroInstance
carroMovelInstance.save()

}

CarroInstance 正在保存,但 carroMovelInstance 没有。我想不明白。任何帮助将不胜感激。

最佳答案

Grooveek is correct因为你从未调用过carroMovelInstance.save() .

但是,您可能更简单地利用 Grails 的数据绑定(bind),而不是不必要地创建关联并手动绑定(bind)参数。

// Update your Carro domain.
def Carro {
String name
String marca
String matricula

// will cause persistence operations to cascade from CarroMovel to Carro
static belongsTo = CarroMovel
}

// Update your save2 action.
// By passing 'params' to the CarroMovel constructor, Grails will bind request
// parameters to domain properties of the same name; it even works with associations!
def save2 = {
def carroMovelInstance = new CarroMovel(params)
if(carroMovelInstance.validate) {
carroMovelInstance.save()
}
}

阅读 Grails Data Binding ,尤其是关于关联的部分。此外,请阅读 "Understanding Cascading Updates and Deletes"了解如何调用 save()在父域对象上将(或不会)级联到关联的域对象。

关于data-binding - Grails 数据绑定(bind)疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6046671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com