如何在 phantom-dsl 中连续更新多个字段?

How to update multiple fields in a row in phantom-dsl?

我正在使用 Phantom 1.26.6。

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

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

// 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)

不知道有没有更好的方法更新多个字段。

在此先感谢您的帮助!

您所要做的就是使用 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()