Grails数据绑定疑问

发布于 2024-11-08 01:55:10 字数 952 浏览 0 评论 0原文

   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 没有保存。我无法弄清楚。任何帮助将不胜感激。

   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()                 

            }

The CarroInstance is saving, but the carroMovelInstance isn't. I cannot figure it out. Any help would be apreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-11-15 01:55:10

Grooveek 是正确的,因为您从未调用过 carroMovelInstance.save( )

然而,对您来说,简单地利用 Grails 的数据绑定可能会更简单,而不是不必要地创建关联并手动绑定参数。

// 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 数据绑定,特别是关联部分。此外,请阅读"了解级联更新和删除" 了解如何对父域对象调用 save() 将(或不会)级联到关联的域对象。

Grooveek is correct in that you haven't ever invoked carroMovelInstance.save().

However, it might be simpler for you to simply take advantage of Grails' databinding, instead of unnecessarily creating the associations and manually binding the parameters.

// 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()
    }
}

Read up on Grails Data Binding, particularly the parts about associations. Additionally, read "Understanding Cascading Updates and Deletes" to understand how a call to save() on a parent domain object will (or will not) cascade to an associated domain object.

纸短情长 2024-11-15 01:55:10

你从不要求 carroMovelInstance 保存... carro 实例也没有引用 carroMovel 实例,因此没有级联保存

you never ask for the carroMovelInstance to save... The carro instance has nor reference to carroMovel instance so there is no cascading of saving

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文