Grails findAll 闭包将查询与 hasmany 字段结合起来
Grails findAll closure combine query with hasmany field
根据以下域 类
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?
}
class VisitScheduleService {
def springSecurityService
void run{
SecUser currentUser = springSecurityService.currentUser
def upcomingVisitSchedules = VisitSchedules.findAll {
visit.startTime >= new DateTime() && users.contains(currentUser)
}
}
}
and... 建筑 VisitSchedules.findAll, VisitSchedules.where and 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)
}
}
}
}
感谢您的回复,它帮了大忙,这是现在的样子。
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)
}
}
但无法实现我所需要的?
class VisitScheduleService {
def springSecurityService
void run{
SecUser currentUser = springSecurityService.currentUser
def upcomingVisits = VisitScheduleNotification.createCriteria().list(){
and{
visit{
ge('startTime', new DateTime())
}
'in'('users', [currentUser])
}
}
}
}
根据以下域 类
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?
}
class VisitScheduleService {
def springSecurityService
void run{
SecUser currentUser = springSecurityService.currentUser
def upcomingVisitSchedules = VisitSchedules.findAll {
visit.startTime >= new DateTime() && users.contains(currentUser)
}
}
}
and... 建筑 VisitSchedules.findAll, VisitSchedules.where and 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)
}
}
}
}
感谢您的回复,它帮了大忙,这是现在的样子。
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)
}
}
但无法实现我所需要的?
class VisitScheduleService {
def springSecurityService
void run{
SecUser currentUser = springSecurityService.currentUser
def upcomingVisits = VisitScheduleNotification.createCriteria().list(){
and{
visit{
ge('startTime', new DateTime())
}
'in'('users', [currentUser])
}
}
}
}