1

我的模型上有一个更新方法,它接受一个带有选项类型的类,我想构造一个更新语句,其中包括一个动态 SET,具体取决于哪些字段具有值。使用 Phanton-dsl 1.5 我有类似的东西;

import com.websudos.phantom.query.{ AssignmentsQuery => AQ }

override def updateModel(m: Upd)(implicit ec: EC): Future[Unit] = {
    if (m.isEmpty) return Future.successful(())
    val upd = update
    upd.where(_.user_id eqs m.user_id)
    val mod: AQ[CTable, Val] = new AQ(this, upd.qb.`with`())
    for (first_name <- m.first_name) mod.and(_.first_name setTo first_name)
    for (last_name <- m.last_name) mod.and(_.last_name setTo last_name)
    mod.future.map(_ => ())
}

现在我正在尝试迁移到 Phantom-dsl (1.27) 的最新版本,但我在仅使用 dsl 进行等效操作时遇到了麻烦。由于任何字段都可能为 None,因此构造第一个 modify() 然后使用任意数量的 and() 进行该操作被证明是困难的。

任何有关如何处理此问题的建议都会有所帮助。

4

1 回答 1

2

这是可能的setIfDefined,你不需要包装任何东西。

db.table.update.where(_.bla eqs bla)
  .modify(_.x setIfDefined None)
  .and(_.y setIfDefined Some("text")

这基本上会忽略所有的东西,None它会迫使它们不在查询中显示。

于 2016-09-15T20:26:54.023 回答