Grails update instead of delete

前端 未结 1 1225
梦谈多话
梦谈多话 2021-02-06 13:43

Is there an easy way in Grails to not allow deleting for any Domain Class? And rather have a delete flag in each domain which gets updated whenever something is deleted.

相关标签:
1条回答
  • 2021-02-06 14:22

    You'll have to ovveride the delete and list methods of all your domain classes. Add code like this to your Bootstrap

    class BootStrap {
    
      def grailsApplication
    
      def init = { servletContext ->
    
      for (dc in grailsApplication.domainClasses) {
    
         dc.clazz.exists(-1);  //to register meta class
    
         def gormSave = dc.clazz.metaClass.getMetaMethod('save');         
         dc.clazz.metaClass.delete = {  ->
            delegate.deleted = true
            gormSave.invoke delegate
         }
    
         dc.clazz.metaClass.delete = { Map args  ->
            delegate.deleted = true
            gormSave.invoke(delegate, args)
         }
    
         dc.clazz.metaClass.static.list = {  ->
            def crit = delegate.createCriteria();
            def list = crit.list{
              eq('deleted', false)
            }
            return list;
         }
    
    
    
      }
    }
    
       def destroy = {}
    }
    
    0 讨论(0)
提交回复
热议问题