开发者

What is the best way to add logic to delete() on Grails domain class?

开发者 https://www.devze.com 2023-04-08 17:55 出处:网络
I need to make changes to other domain classes when an instance of a particular domain class is deleted. What is the best way to do this? I don\'t want to wait until commit or flush so I don\'t think

I need to make changes to other domain classes when an instance of a particular domain class is deleted. What is the best way to do this? I don't want to wait until commit or flush so I don't think the "beforeDelete" callback will help. I would like to "override" delete, do some stuff and call super.delete():

class Foo {
    Bar bar
    void delete() {
        if (bar) bar.foo = null
        super.delete() -- this doesn't work
    }
}

Currently I have named "delete" cancel but would like to call it "del开发者_StackOverflow中文版ete" but then I cannot call the original delete().


To add to what @sbglasius said, here's the link to the docs on GORM events

Complete example:

class Foo {
    Bar bar

    def beforeDelete() {
        if(bar) {
            bar.foo = null
        }
    }
}


I haven't tried overriding GORM methods myself, but this might give some insight on what's involved:

"Overloading" standard GORM CRUD methods

I would put the "delete" logic in a service and call that instead:

class FooService {

    def deleteInstance(foo) {
        if (foo?.bar) {
            foo.bar.foo = null
            // might have to call foo.bar.save() here
            // then foo.bar = null
        }
        foo.delete()
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消