Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails从父域模型更新一对多_Grails - Fatal编程技术网

Grails从父域模型更新一对多

Grails从父域模型更新一对多,grails,Grails,我正在更新控制器中父域模型中的一对多 代码如下所示: def update() { def parentInstance = Parent.get(params.id) params.childerenDetails.each { parentInstance.addToChildrens( newChildren( name:params.get(name_"${it}"), age:params.

我正在更新控制器中父域模型中的一对多

代码如下所示:

def update() {

   def parentInstance = Parent.get(params.id)

   params.childerenDetails.each {

      parentInstance.addToChildrens(
         newChildren(
            name:params.get(name_"${it}"),
            age:params.get(age_"${it}"))
      }
   }

   if(parentInstance.validate()) {
      parentInstance.save(flush:true)
   } else {
      render "error found at"+parentInstance.errors
   }
}

..在这里,当添加子值时,我在父类中进行了自定义验证,以获得父验证错误,但子对象正在保存到数据库中..如何防止验证错误出现在父域中

如果要防止子对象存储到数据库中,则需要将此代码移动到服务中,默认情况下是事务性的,或将所有代码封装在Parent.withTransaction{/}内。如果选择使用该服务,则可以在检测到验证错误时引发RuntimeException以强制回滚。如果使用withTransaction,则可以使用status手动回滚它。setRollbackOnly:

不确定parentInstance.saveflush:true、failOnError:true是否也会触发回滚。 不过,我更愿意将业务逻辑转移到服务。请参阅Ref指南中的和文档

def parentInstance = Parent.get(params.id)
Parent.withTransaction { status ->
   params.childerenDetails.each {

     parentInstance.addToChildrens(
        newChildren(
           name:params.get(name_"${it}"),
           age:params.get(age_"${it}"))
       }
   }

   if(parentInstance.validate()) {
      parentInstance.save(flush:true)
   } else {
     status.setRollbackOnly()
   }
}