在我的域类SupplierGroup中,我有很多关联。在我的域类中,例如:供应商
int id
String supplierGroupName
Long orgId
Date dateCreated
static hasMany = [suppliers:OrganisationUnit] ..
我想捕获每个修改后的属性名称的旧值和新值。 但总是得到供应商属性名称的当前值。这是我不想要的。
def beforeUpdate(){
println "BEFORE UPDATE: " + this.getPersistentValue('suppliers');
}
def afterUpdate(){
println "AFTER UPDATE: " + this.suppliers;
}
BEFORE UPDATE:[com.xms.core.organisation.OrganisationUnit(13,ABC),com.xms.core.organisation.OrganisationUnit(14,DEF)]
更新后:[com.xms.core.organisation.OrganisationUnit(13,ABC),com.xms.core.organisation.OrganisationUnit(14,DEF)]
Grails收集关联道具名称的所有旧值和新值的最佳方法是什么,主要是供应商?我目前在控制器层中的方法如下:
def multiOldIds = ""
def multiNewIds = ""
Block 1 :
supplierGroupInstance.suppliers.each(){ supplier ->
if(multiOldIds != ""){
multiOldIds += ","
}
//gets me the comma separated (ex: 1,2,3)
multiOldIds += supplier.id
}
第2座:
supplierGroupInstance.suppliers.each(){ supplier ->
ifmultiNewIds != ""){
multiNewIds += ","
}
//gets me the comma separated (ex: 1,2,3)
multiNewIds += supplier.id
}
块1在supplierGroupInstance.save(flush:true)之前执行,而block 2在save flush之后执行。
然后将旧数据和新数据保存到db ..
if(!(multiOriginalIds.equals(multiNewIds))){
actionLogService.savePreviousAndCurrentData(domainClass,id,multiOriginalIds,multiOriginalIdsCurrent,"suppliers")
}
因此总结一下.. 我的方法是否更好地捕获hasMany道具的旧值和新值?或者是否有其他方法..请建议。
..........
//Performs the Update method on the Domain
def update(Long id, Long version) {
if(domainDebug) println domainName+"update() :params passed ${params}"
if(domainHasUpdatedByXmsLoginId){
params.updatedByXmsLoginId = sec.loggedInUserInfo(field:"id")
}
def multiOldIds = ""
def dirtyPropertyNames = ""
def multiNewIds = ""
def supplierGroupInstance = SupplierGroup.get(id)
String domainClass = this.class.getSimpleName().minus("Controller")
//set the hasmany porperty values
supplierGroupInstance.suppliers.each(){ supplier ->
if(multiOldIds != ""){
multiOldIds += ","
}
//gets me the comma separated (ex: 1,2,3)
multiOldIds += supplier.id
}
if (version != null) {
if (supplierGroupInstance.version > version) {
supplierGroupInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'supplierGroup.label', default: 'SupplierGroup')] as Object[],
"Another user has updated this SupplierGroup while you were editing")
render(view: "edit", model: [supplierGroupInstance: supplierGroupInstance])
return
}
}
supplierGroupInstance.properties = params
//below section is to retrive old and new values for primitive type propertyname
dirtyPropertyNames = supplierGroupInstance.getDirtyPropertyNames()
if(dirtyPropertyNames != null && dirtyPropertyNames.size() >0 && id !=null){
for (dirtyPropertyName in dirtyPropertyNames) {
def originalValue = supplierGroupInstance.getPersistentValue(dirtyPropertyName)
def currentValue = supplierGroupInstance."${dirtyPropertyName}"
// println "current value----> " +currentValue
// println "original value----->" + originalValue
if(!(originalValue.equals(currentValue))){
actionLogService.savePreviousAndCurrentData(domainClass,id,originalValue,currentValue,dirtyPropertyName)
}
}
}
if (!supplierGroupInstance.save(flush: true)) {
render(view: "edit", model: [supplierGroupInstance: supplierGroupInstance])
return
}
supplierGroupInstance.suppliers.each(){ supplier ->
if(multiNewIds != ""){
multiNewIds += ","
}
multiNewIds += supplier.id
}
if(!(multiOldIds.equals(multiNewIds))){
actionLogService.savePreviousAndCurrentData(domainClass,id,multiOriginalIds,multiOriginalIdsCurrent,"suppliers")
}
flash.message = message(code: 'default.updated.message', args: [message(code: 'supplierGroup.label', default: 'SupplierGroup'), supplierGroupInstance.id])
if(domainHasLastUpdated){
redirect(action: "list", params: [sort:"lastUpdated", order:"desc"])
} else {
redirect(action: "list")
}
}
答案 0 :(得分:2)
<{p>
getPersistentValue
适用于域名字段,该字段从hibernate缓存中提取域的原始状态。话虽如此,如果你这样做,这将有用
在供应商/ OrganizationUint结束时beforeUpdate()。