是否可以爲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