防止从 Grails 控制器写入数据库
Preventing a write to the database from Grails controller
我正在编写一个小的 Grails 项目作为学习练习。它从表单收集一些用户输入(例如,输入两个数字进行相加),然后调用服务来处理该数据(例如,将两个数字相加),最后在另一个页面上显示结果。
当我打开 SQL 日志记录时,我注意到在调用控制器内的服务方法之前,用户输入的数据已保存到数据库中。
如何防止这种情况发生?我想在调用服务方法完成并且没有错误后写入数据库。
控制器代码中的保存方法:
def save() {
def myInstance = new myDomainClass(params)
myInstance.sessionId = session.id
myService argh = new myService()
// wtf does this do?
if (!myInstance.save(flush: true)) {
render(view: "create", model: [myInstance: myInstance])
return
}
// Execute the api and process the results. what is writing the user input to the database before this call?!?!?!?!
def results1 = argh.executeApi(myInstance)
// if the results are null throw an error
if (results1 == null) {
flash.message = message(code: 'api.null.error')
render(view: "create", model: [apiInstance: apiInstance])
return
} else {
forward(action: "list", id: 2, model: [apiInstanceList: Api.list(params), apiInstanceTotal: Api.count()])
}
}
感谢指点或帮助。
调用.save(flush:true)
会在此时自动将 myInstance 实例保存到数据库中。您需要将 .save(flush:true)
移动到服务方法之后,并且由于您说过要确保没有错误,因此您需要将其添加到条件中:
def save() {
def myInstance = new myDomainClass(params)
myInstance.sessionId = session.id
myService argh = new myService()
// Execute the api and process the results. what is writing the user input to the database before this call?!?!?!?!
def results1 = argh.executeApi(myInstance)
// if the results are null throw an error
if (results1 == null) {
flash.message = message(code: 'api.null.error')
render(view: "create", model: [apiInstance: apiInstance])
return
} else {
// save here when you know there are no errors
if (!myInstance.save(flush: true)) {
render(view: "create", model: [myInstance: myInstance])
return
}
forward(action: "list", id: 2, model: [apiInstanceList: Api.list(params), apiInstanceTotal: Api.count()])
}
}
我正在编写一个小的 Grails 项目作为学习练习。它从表单收集一些用户输入(例如,输入两个数字进行相加),然后调用服务来处理该数据(例如,将两个数字相加),最后在另一个页面上显示结果。
当我打开 SQL 日志记录时,我注意到在调用控制器内的服务方法之前,用户输入的数据已保存到数据库中。
如何防止这种情况发生?我想在调用服务方法完成并且没有错误后写入数据库。
控制器代码中的保存方法:
def save() {
def myInstance = new myDomainClass(params)
myInstance.sessionId = session.id
myService argh = new myService()
// wtf does this do?
if (!myInstance.save(flush: true)) {
render(view: "create", model: [myInstance: myInstance])
return
}
// Execute the api and process the results. what is writing the user input to the database before this call?!?!?!?!
def results1 = argh.executeApi(myInstance)
// if the results are null throw an error
if (results1 == null) {
flash.message = message(code: 'api.null.error')
render(view: "create", model: [apiInstance: apiInstance])
return
} else {
forward(action: "list", id: 2, model: [apiInstanceList: Api.list(params), apiInstanceTotal: Api.count()])
}
}
感谢指点或帮助。
调用.save(flush:true)
会在此时自动将 myInstance 实例保存到数据库中。您需要将 .save(flush:true)
移动到服务方法之后,并且由于您说过要确保没有错误,因此您需要将其添加到条件中:
def save() {
def myInstance = new myDomainClass(params)
myInstance.sessionId = session.id
myService argh = new myService()
// Execute the api and process the results. what is writing the user input to the database before this call?!?!?!?!
def results1 = argh.executeApi(myInstance)
// if the results are null throw an error
if (results1 == null) {
flash.message = message(code: 'api.null.error')
render(view: "create", model: [apiInstance: apiInstance])
return
} else {
// save here when you know there are no errors
if (!myInstance.save(flush: true)) {
render(view: "create", model: [myInstance: myInstance])
return
}
forward(action: "list", id: 2, model: [apiInstanceList: Api.list(params), apiInstanceTotal: Api.count()])
}
}