0

我有下一个方案:

class UserProfile {
    String title
    String firstName
    String lastName 
    static belongsTo = [user:User]
    static constraints = {
            user nullable:true , unique:true
            title nullable:true, blank:true
            firstName blank:false
            lastName nullable:true, blank:true
    }
}

class User {
    String username
    String password
    boolean enabled 
    String email    
    static constraints = {
        username size:6..40, blank:false, nullable: false, unique: true
        email email:true, size:6..40, blank:false, nullable: false, unique: true
        password size:5..64, password:true, blank:false, nullable: false
    }   
    String toString(){username}
}

我需要UserProfile有用户的电子邮件订购的列表!

我尝试:

UserProfile.createCriteria().listDistinct({
    if(params.orderBy){
        if(params.orderBy=="email"){
        //the exception says that the element has no such property to both cases    
            order("user.email", ascDesc)
            //order("email", ascDesc)               
        }else{
            order("${params.orderBy}", ascDesc)
        }
    }

})

所以当我想通过电子邮件订购时,异常表明该元素对这两种情况都没有这样的属性。注意:ascDesc是一个变量String,它可以是ascdesc

4

2 回答 2

1

如果你添加

createAlias('user', 'u')

到您的标准关闭的顶部,那么您应该可以按'u.email'.

于 2012-09-18T18:35:56.867 回答
1
if (params.orderBy == "email") { 
    user { 
        order ("email", ascDesc) 
    } 
}

无需手动创建别名也应该可以工作

于 2012-09-23T11:50:29.223 回答