为什么当我删除 grails 上一对多关系上的父级时,会在子级上调用 beforeInsert 事件?

发布于 2024-08-31 08:32:34 字数 4603 浏览 7 评论 0原文

我有一对多关系,当我尝试删除具有多个子项的父项时,第一个子项会调用 berforeInsert 事件。在这种情况下,我有一些代码,我打算在插入子项之前调用,而不是在删除父项时调用!关于可能出什么问题的任何想法吗?

实体:

class MenuItem {

    static constraints = {
        name(blank:false,maxSize:200)
                category()
                subCategory(nullable:true, validator:{
                        val, obj ->
                        if(val == null){                            
                            return true
                        }else{                            
                            return obj.category.subCategories.contains(val)? true : ['invalid.category.no.subcategory']
                        }
                })
        price(nullable:true)    
        servedAtSantaMonica()
        servedAtWestHollywood()     
        highLight()
        servedAllDay()                
        dateCreated(display:false)
        lastUpdated(display:false)
    }

    static mapping = {
        extras lazy:false
    }

    static belongsTo = [category:MenuCategory,subCategory:MenuSubCategory]
    static hasMany = [extras:MenuItemExtra]

    static searchable = {
       extras component: true
    }

    String name
    BigDecimal price
    Boolean highLight = false
    Boolean servedAtSantaMonica = false
    Boolean servedAtWestHollywood = false
    Boolean servedAllDay = false
    Date dateCreated
    Date lastUpdated
    int displayPosition

     void moveUpDisplayPos(){
        def oldDisplayPos = MenuItem.get(id).displayPosition
        if(oldDisplayPos == 0){
            return
        }else{
            def previousItem = MenuItem.findByCategoryAndDisplayPosition(category,oldDisplayPos - 1)
            previousItem.displayPosition += 1
            this.displayPosition = oldDisplayPos - 1
            this.save(flush:true)
            previousItem.save(flush:true)
        }
    }

    void moveDownDisplayPos(){
        def oldDisplayPos = MenuItem.get(id).displayPosition
        if(oldDisplayPos == MenuItem.countByCategory(category) - 1){
            return
        }else{
            def nextItem = MenuItem.findByCategoryAndDisplayPosition(category,oldDisplayPos + 1)
            nextItem.displayPosition -= 1
            this.displayPosition = oldDisplayPos + 1
            this.save(flush:true)
            nextItem.save(flush:true)
        }
    }

    String toString(){
            name
    }

    def beforeInsert = {
       displayPosition = MenuItem.countByCategory(category)
    }

    def afterDelete = {

        def otherItems = MenuItem.findAllByCategoryAndDisplayPositionGreaterThan(category,displayPosition)

        otherItems.each{
            it.displayPosition -= 1
            it.save()
        }


    }
}




    class MenuItemExtra {

        static constraints = {
            extraOption(blank:false, maxSize:200)
            extraOptionPrice(nullable:true)

        }

        static searchable = true

        static belongsTo = [menuItem:MenuItem]

        BigDecimal extraOptionPrice
        String extraOption
        int displayPosition

        void moveUpDisplayPos(){
            def oldDisplayPos = MenuItemExtra.get(id).displayPosition
            if(oldDisplayPos == 0){
                return
            }else{
                def previousExtra = MenuItemExtra.findByMenuItemAndDisplayPosition(menuItem,oldDisplayPos - 1)
                previousExtra.displayPosition += 1
                this.displayPosition = oldDisplayPos - 1
                this.save(flush:true)
                previousExtra.save(flush:true)
            }
        }

        void moveDownDisplayPos(){
            def oldDisplayPos = MenuItemExtra.get(id).displayPosition
            if(oldDisplayPos == MenuItemExtra.countByMenuItem(menuItem) - 1){
                return
            }else{
                def nextExtra = MenuItemExtra.findByMenuItemAndDisplayPosition(menuItem,oldDisplayPos + 1)
                nextExtra.displayPosition -= 1
                this.displayPosition = oldDisplayPos + 1
                this.save(flush:true)
                nextExtra.save(flush:true)
            }
        }

        String toString(){
            extraOption
        }

        def beforeInsert = {
           if(menuItem){
                displayPosition = MenuItemExtra.countByMenuItem(menuItem)
           }
        }

        def afterDelete = {

            def otherExtras = MenuItemExtra.findAllByMenuItemAndDisplayPositionGreaterThan(menuItem,displayPosition)

            otherExtras.each{
                it.displayPosition -= 1
                it.save()
            }     

        }



    }

I have a one to many relationship and when I try to delete a parent that haves more than one child the berforeInsert event gets called on the frst child. I have some code in this event that I mean to call before inserting a child, not when i'm deleting the parent! any ideas on what might be wrong?

the entities:

class MenuItem {

    static constraints = {
        name(blank:false,maxSize:200)
                category()
                subCategory(nullable:true, validator:{
                        val, obj ->
                        if(val == null){                            
                            return true
                        }else{                            
                            return obj.category.subCategories.contains(val)? true : ['invalid.category.no.subcategory']
                        }
                })
        price(nullable:true)    
        servedAtSantaMonica()
        servedAtWestHollywood()     
        highLight()
        servedAllDay()                
        dateCreated(display:false)
        lastUpdated(display:false)
    }

    static mapping = {
        extras lazy:false
    }

    static belongsTo = [category:MenuCategory,subCategory:MenuSubCategory]
    static hasMany = [extras:MenuItemExtra]

    static searchable = {
       extras component: true
    }

    String name
    BigDecimal price
    Boolean highLight = false
    Boolean servedAtSantaMonica = false
    Boolean servedAtWestHollywood = false
    Boolean servedAllDay = false
    Date dateCreated
    Date lastUpdated
    int displayPosition

     void moveUpDisplayPos(){
        def oldDisplayPos = MenuItem.get(id).displayPosition
        if(oldDisplayPos == 0){
            return
        }else{
            def previousItem = MenuItem.findByCategoryAndDisplayPosition(category,oldDisplayPos - 1)
            previousItem.displayPosition += 1
            this.displayPosition = oldDisplayPos - 1
            this.save(flush:true)
            previousItem.save(flush:true)
        }
    }

    void moveDownDisplayPos(){
        def oldDisplayPos = MenuItem.get(id).displayPosition
        if(oldDisplayPos == MenuItem.countByCategory(category) - 1){
            return
        }else{
            def nextItem = MenuItem.findByCategoryAndDisplayPosition(category,oldDisplayPos + 1)
            nextItem.displayPosition -= 1
            this.displayPosition = oldDisplayPos + 1
            this.save(flush:true)
            nextItem.save(flush:true)
        }
    }

    String toString(){
            name
    }

    def beforeInsert = {
       displayPosition = MenuItem.countByCategory(category)
    }

    def afterDelete = {

        def otherItems = MenuItem.findAllByCategoryAndDisplayPositionGreaterThan(category,displayPosition)

        otherItems.each{
            it.displayPosition -= 1
            it.save()
        }


    }
}




    class MenuItemExtra {

        static constraints = {
            extraOption(blank:false, maxSize:200)
            extraOptionPrice(nullable:true)

        }

        static searchable = true

        static belongsTo = [menuItem:MenuItem]

        BigDecimal extraOptionPrice
        String extraOption
        int displayPosition

        void moveUpDisplayPos(){
            def oldDisplayPos = MenuItemExtra.get(id).displayPosition
            if(oldDisplayPos == 0){
                return
            }else{
                def previousExtra = MenuItemExtra.findByMenuItemAndDisplayPosition(menuItem,oldDisplayPos - 1)
                previousExtra.displayPosition += 1
                this.displayPosition = oldDisplayPos - 1
                this.save(flush:true)
                previousExtra.save(flush:true)
            }
        }

        void moveDownDisplayPos(){
            def oldDisplayPos = MenuItemExtra.get(id).displayPosition
            if(oldDisplayPos == MenuItemExtra.countByMenuItem(menuItem) - 1){
                return
            }else{
                def nextExtra = MenuItemExtra.findByMenuItemAndDisplayPosition(menuItem,oldDisplayPos + 1)
                nextExtra.displayPosition -= 1
                this.displayPosition = oldDisplayPos + 1
                this.save(flush:true)
                nextExtra.save(flush:true)
            }
        }

        String toString(){
            extraOption
        }

        def beforeInsert = {
           if(menuItem){
                displayPosition = MenuItemExtra.countByMenuItem(menuItem)
           }
        }

        def afterDelete = {

            def otherExtras = MenuItemExtra.findAllByMenuItemAndDisplayPositionGreaterThan(menuItem,displayPosition)

            otherExtras.each{
                it.displayPosition -= 1
                it.save()
            }     

        }



    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-09-07 08:32:34

我相信我的错误与删除关系的“多方”有关。因为在 afterDelete 上,我修改了具有相同父级的其他实体,然后调用可能触发 beforeInsert 的 save() 方法。

也许我有一个概念错误,并且修改具有同一父级的实体,在删除其中一个实体后必须在其他地方完成......我不知道。

I believe my mistake has to do with the afterDelete of the "many side" of the relationship. Because on the afterDelete I modify the other entities that have the same parent and then call the save() method that might be triggering the beforeInsert.

Maybe I have a conceptual mistake and the modifying of enitities with the same parent, after a single deletion of one of them has to be done some where else...i don't know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文