Grails 如何实现软删除
有人能告诉我什么是实现软删除的好方法吗?我的类中可以有一个Grails 如何实现软删除,grails,gorm,grails-domain-class,Grails,Gorm,Grails Domain Class,有人能告诉我什么是实现软删除的好方法吗?我的类中可以有一个deleted属性,但我的问题是如何轻松忽略在搜索、列表等中有deleted=true的实例 因此,与其说Domain.findByDeleted(true)不如说getDomain.list()忽略已删除的实例,而不要说Domain.findByProperty和ddeleted('property',true)只要说Domain.findByProperty('property') 有什么好办法吗?我建议您使用a。差不多 static
deleted
属性,但我的问题是如何轻松忽略在搜索、列表等中有deleted=true
的实例
因此,与其说Domain.findByDeleted(true)
不如说getDomain.list()
忽略已删除的实例,而不要说Domain.findByProperty和ddeleted('property',true)
只要说Domain.findByProperty('property')
有什么好办法吗?我建议您使用a。差不多
static namedQueries = {
notDeleted {
ne 'deleted', true
}
}
您可以使用它,如
Domain.notDeleted.list()
或Domain.notDeleted.findByProperty(value)
我们用来覆盖list()
,get()
和一些域类的其他方法。现在我们可以使用类似A.delete(log:true)
在引导过程中,我们会:
grailsApplication.domainClasses.each { GrailsDomainClass domainClassInfo ->
def oldGormDelete = domainClassInfo.metaClass.getMetaMethod('delete', [] as Class[])
assert oldGormDelete
domainClassInfo.metaClass.delete = { Map params ->
...
def result = oldGormDelete.invoke(delegate)
...
}
}
可以将谓词
deleted=false
自动添加到为特定域类执行的每个查询中。然而,我的测试表明该插件不能与Grails2.0.0一起使用。这是我第二次遇到这种策略。我不太熟悉grails元编程,您能解释一下调用委托的过程吗?我正在使用Groovy元编程(请参阅“ExpandoMetaClass”一节)。在类“metaClass
属性中,我指定要调用的闭包,而不是某些方法签名,在示例-staticdelete(Map params)
method中delegate
是一个Groovy关键字,类似于this
和owner
,这意味着闭包被称为方法的对象-请参阅invoke()
是,呃,Closure
用给定的this
和给定的参数列表执行它的方法-第一个参数后面的参数将用作调用参数。这应该可以帮助您: