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()
            }     

        }



    }
4

1 回答 1

0

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

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

于 2010-05-07T20:13:00.133 回答