Có thể tự động tạo danh sách cho phương thức "bật" của anorm không?Tham số SQL động với Anorm và Scala Play Framework
Tôi có một biểu mẫu với các đầu vào tùy chọn và hiện tại tôi kiểm tra từng tùy chọn và tạo danh sách với các tùy chọn được xác định và đang cố gắng chuyển thông tin này đến dạng anorm. Hiện tại tôi nhận được lỗi biên dịch này
type mismatch; found : List[java.io.Serializable] required: (Any, anorm.ParameterValue[_])
Tôi không chắc chắn về cách tạo danh sách này. đang hiện tại:
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 *)
Tôi đã cố gắng làm điều này vì ban đầu tôi nghĩ rằng nó sẽ được giống như
.on('rooms -> input.rooms, 'bathroom -> input.bathrooms... etc)
EDIT:
Mã bây giờ là:
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[_])]]
sử dụng lệnh SQL:
SQL("SELECT * FROM Houses WHERE " + whereString).on(onList:_*).as(sqlToHouse *)
Bây giờ nhận được ngoại lệ
[ClassCastException: java.lang.Integer cannot be cast to anorm.ParameterValue]
Tính năng này hoạt động như thế nào? 'WhereString' trông như thế nào? –