在 Grails Audit Loggin 插件的 onChange 方法中,如何获取对所属可审核域对象的引用?
我已经成功地让 grails 审核日志记录插件正常工作,看起来正是我所需要的,只是我无法弄清楚如何从 onChange 方法中获取对可审核域对象的引用。下面是插件示例 Person 类的代码,还有几行我想要实现的目标:
class Person {
static auditable = true
Long id
Long version
String firstName
String middleName
String lastName
String email
static hasMany = [emailRecords : EmailRecord]
static constraints = {
firstName(nullable:true,size:0..60)
middleName(nullable:true,size:0..60)
lastName(nullable:false,size:1..60)
email(email:true)
}
def onSave = {
println "new person inserted" // may optionally refer to newState map
}
def onDelete = {
println "person was deleted" // may optionally refer to oldState map
}
def onChange = {
oldMap,newMap ->
println "Person was changed ..."
oldMap.each({ key, oldVal ->
if(oldVal != newMap[key]) {
println " * $key changed from $oldVal to " + newMap[key]
// how can achieve something like this?
if(key == "email"){
def personInstance = this // this didn't work, I'm not sure how I can get such a reference to the owning domain object
personInstance.addToEmailRecords(
new EmailRecord(
email:newMap[key],
date: new Date()
).save()
)
}
}
})
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这个用例,您可能真的只想使用 标准 GORM 事件 使用 isDirty() 和 getPersistentValue() 至少进行更新。特别是,如审计日志插件文档中所述,它旨在在实体提交到数据存储后对其进行操作(因此,例如,保证分配对象 ID)。
尝试如下操作:
这样,在提交更改后,您就不应该因修改对象而遇到任何麻烦。
For this use-case, you probably really just want to use the standard GORM events using isDirty() and getPersistentValue() at least to make the updates. In particular, as noted in the documentation for the audit-logging plugin, it is designed to work on entities after they have been committed to the data store (and so, for example, the object id is guaranteed to be assigned).
Try something like the following:
That way you shouldn't hit into any funkiness with modifying the object after it's changes have been committed.