是否可以为anorm的“on”方法动态创建一个列表?带有Anorm和Scala Play Framework的动态SQL参数
我有一个可选输入的表单,目前我检查每个选项,并用定义的选项创建一个列表,并试图将其传递给anorm。目前,我得到这个编译错误
type mismatch; found : List[java.io.Serializable] required: (Any, anorm.ParameterValue[_])
我不知道我怎么会去有关创建这个列表。 当前代码:
val onList = List(
'school_id = input.school,
if(input.rooms isDefined) ('rooms -> input.rooms) else "None" ,
if(input.bathrooms isDefined) ('bathrooms -> input.bathrooms) else "None" ,
if(input.houseType isDefined) ('houseType -> input.houseType) else "None" ,
if(input.priceLow isDefined) ('priceLow -> input.priceLow) else "None" ,
if(input.priceHigh isDefined) ('priceHigh -> input.priceHigh) else "None" ,
if(input.utilities isDefined) ('utilities -> input.utilities) else "None"
).filter(_!="None")
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList).as(sqlToHouse *)
我已经试过这样做,因为我最初认为这将是一样
.on('rooms -> input.rooms, 'bathroom -> input.bathrooms... etc)
编辑:现在
代码是:
val onList = Seq(
('school_id -> input.school),
if(input.rooms isDefined) ('rooms -> input.rooms.get) else None ,
if(input.bathrooms isDefined) ('bathrooms -> input.bathrooms.get) else None ,
if(input.houseType isDefined) ('houseType -> input.houseType.get) else None ,
if(input.priceLow isDefined) ('priceLow -> input.priceLow.get) else None ,
if(input.priceHigh isDefined) ('priceHigh -> input.priceHigh.get) else None ,
if(input.utilities isDefined) ('utilities -> input.utilities.get) else None
).filter(_!=None).asInstanceOf[Seq[(Any,anorm.ParameterValue[_])]]
使用SQL命令:
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList:_*).as(sqlToHouse *)
现在越来越异常
[ClassCastException: java.lang.Integer cannot be cast to anorm.ParameterValue]
这应该如何工作? 'whereString'看起来像什么? – 2013-08-14 16:42:32