Grails findAll closure将查询与hasmany字段组​​合在一起

时间:2015-03-01 06:32:46

标签: grails groovy gorm findall

根据以下域类

VisitSchedules{
    Visit visit
    static hasMany = [users: User]
    ...
}

Visit{
    DateTime startTime
    ...
}

我想查询VisitSchedules,了解为用户的未来日期安排的访问。 类似下面的内容

def upcomingVisitSchedules = VisitSchedules.findAll {
    visit.startTime >= new DateTime() && users.contains(user)//How do I query if the users list contains the current user?
}

4 个答案:

答案 0 :(得分:1)

和...构建VisitSchedules.findAll,VisitSchedules.where和VisitSchedules.withCriteria不是自给自足的,也不可靠。始终使用构造VisitSchedules.createCriteria()。

class VisitScheduleService {

   def springSecurityService

   void run{
    SecUser currentUser = springSecurityService.currentUser

    def upcomingVisitSchedules = VisitSchedules.createCriteria().list() {
       and {
          visit {
             le('startTime', new DateTime())
          }
          eq('user', currentUser)
       }
    }  
  }

}

答案 1 :(得分:0)

class VisitScheduleService {

   def springSecurityService

   void run{

    SecUser currentUser = springSecurityService.currentUser

    def upcomingVisitSchedules = VisitSchedules.findAll {
            visit.startTime >= new DateTime() && users.contains(currentUser)
    }

   }

}

答案 2 :(得分:0)

感谢您的回复,它帮助了很多,现在看来它的样子。

class VisitScheduleService {
    def springSecurityService
    void run{
        SecUser currentUser = springSecurityService.currentUser
        def upcomingVisits = VisitScheduleNotification.createCriteria().list(){
            and{
                visit{
                    ge('startTime', new DateTime())
                }
                users{
                    eq('id', user.id)
                }
            }
        }
    }
}

我有一个问题: 问题:我正在尝试使用'in'("users",user)条件方法在属性中查找用户static hasMany = [users: User]

def upcomingVisits = VisitScheduleNotification.createCriteria().list(){
            and{
                visit{
                    ge('startTime', new DateTime())
               }
               'in'('users', user)
            }
        }

但无法实现我所需要的目标?

答案 3 :(得分:0)

class VisitScheduleService {
    def springSecurityService
    void run{
        SecUser currentUser = springSecurityService.currentUser
        def upcomingVisits = VisitScheduleNotification.createCriteria().list(){
            and{
                visit{
                    ge('startTime', new DateTime())
                }            
                'in'('users', [currentUser])
            }
        }
    }
}