【问题标题】:How to update multiple fields in a row in phantom-dsl?如何在 phantom-dsl 中连续更新多个字段?
【发布时间】:2016-11-05 12:46:27
【问题描述】:

我正在使用 Phantom 1.26.6。

// id is the primary key
case class Motorcycle(id:String, model:String, made:String, capacity:Int)

给出一个已存在于 Cassandra 中的 Motorcycle 实例,我会 喜欢更新模型、制造、容量的值。

// The following does not compile.
 update.where(_.id.eqs(bike.id)).modify(_.model.setTo(bike.model))
 .modify(_.make.setTo(bike.make))
 .modify(_.capacity.setTo(bike.capacity))


 // The following works.   
 val updateQuery = update.where(_.id.eqs(bike.id))

 for {
    _ <- updateQuery.modify(_.model.setTo(bike.model)).future()
    _ <- updateQuery.modify(_.make.setTo(bike.made)).future()
    result <- updateQuery.modify(_.capacity.setTo(bike.capacity)).future()
  } yield (result)

我想知道是否有更好的方法来更新多个字段。

提前感谢您的帮助!

【问题讨论】:

    标签: scala cassandra phantom-dsl


    【解决方案1】:

    您所要做的就是使用and 运算符链接多个更新语句。这将在单个查询中执行所有内容。

     val updateQuery = update.where(_.id eqs bike.id)
       .modify(_model setTo bike.model)
       .and(_.make setTo bike.made)
       .and(_.capacity setTo bike.capacity)
       .future()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2014-12-02
      • 1970-01-01
      • 2012-06-29
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多