在Mapper.findAll中传递动态QueryParams

时间:2013-01-25 10:12:58

标签: scala lift

我有这样的功能:

def getList(gig: Gig, createdBy: User = null, all: Boolean = true): List[Bid] = {
    var bys = List[QueryParam](By(Bid.gig, gig))
    if (createdBy!=null) bys = By(Bid.createdBy, createdBy) :: bys
    if (!all) bys = By(Bid.deleted, false) :: bys

    Bid.findAll(bys) //gives error as do not accept List[QueryParam]
  }

如何向findAll提供动态数量的QueryParams?

1 个答案:

答案 0 :(得分:0)

像这样:

Bid.findAll(bys:_*)

旁注:值得避免使用nullOption

def getList(gig: Gig, createdBy: Option[User] = None, all: Boolean = true): List[Bid] = {

然后你可以做

val bys = (if (!all) Some(By(Bid.deleted, false)) else None) ++
          (createdBy.map(u => By(Bid.createdBy, u))) ++ 
          List[QueryParam](By(Bid.gig, gig))
Bid.findAll(bys:_*)