使用params查询关联的域对象会产生错误:无法查询属性“params” - 类上没有此类属性

时间:2014-06-03 22:32:05

标签: grails gorm

我有两个域对象: HeadOfHousehold和地址,

在HeadOfHousehold我有以下关系:

Address currentAddress
Address previousAddress

在地址中我有:

static belongsTo = [headOfHousehold: HeadOfHousehold]

我有以下查询:

def customers = HeadOfHousehold.findAll {
            if(params.lastName){
                lastName =~ params.lastName+ "%"
            }
            if(params.firstName){
                firstName =~ params.firstName + "%"
            }
            if(params.middleName){
                middleName =~ params.middleName + "%"
            }
            if(params.city){
                currentAddress.city =~ params.city+ "%"
            }
            if(params.zipCode){
                currentAddress.zipCode == params.zipCode
            }
            if(params.extendedZipCode){
                currentAddress.extendedZipCode == params.extendedZipCode
            }

        }

问题在于我得到错误的最后三个语句:

Cannot query property "params" - no such property on class HeadOfHousehold

当我进入该地址时,它不会抱怨与lastName firstName或middleName相关联的查询。我可以通过将字符串保存在字符串中并以这种方式绕过它:

String city = params.city + "%"
        String zipCode = params.zipCode
        String extendedZipCode = params.extendedZipCode
        String state = params.state
        def customers = HeadOfHousehold.findAll {
            if(params.lastName){
                lastName =~ params.lastName+ "%"
            }
            if(params.firstName){
                firstName =~ params.firstName + "%"
            }
            if(params.middleName){
                middleName =~ params.middleName + "%"
            }
            if(params.city){
                currentAddress.city =~ city 
            }
            if(params.state){
                currentAddress.state == state
            }
            if(params.zipCode){
                currentAddress.zipCode == zipCode
            }
            if(params.extendedZipCode){
                currentAddress.extendedZipCode == extendedZipCode
            }

        }

为什么这有必要?我究竟做错了什么? (使用grails版本2.3.8)

1 个答案:

答案 0 :(得分:4)

尝试匹配关联或属性的属性,它将params看作父类的属性。我不完全确定这是否是预期的行为。我会更多地研究它。

但是,与.操作相比,您可以通过使用您所采用的方式或仅使用闭包方法(DSL方式)来克服此问题。例如:

if(params.city){
    currentAddress { city =~ params.city+ "%" }
}
if(params.zipCode){
    currentAddress { zipCode == params.zipCode } 
}
if(params.extendedZipCode){
    currentAddress { extendedZipCode == params.extendedZipCode }
}