Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 平滑动态更新字段_Scala_Playframework_Slick - Fatal编程技术网

Scala 平滑动态更新字段

Scala 平滑动态更新字段,scala,playframework,slick,Scala,Playframework,Slick,我从Play控制器获得一张表单 class CustomerTable(tag: Tag) extends Table[Customer]("customer", tag) { def id = column[Long]("id") ... def * = ... } case class Customer( id: Long, remarkName: String ... ) case class CustomerUpdateForm( id: Lo

我从
Play
控制器获得一张表单

class CustomerTable(tag: Tag) extends Table[Customer]("customer", tag) {
   def id = column[Long]("id")
   ...
   def * = ...
}

case class Customer(
   id: Long,
   remarkName: String
   ...
)

case class CustomerUpdateForm(
  id: Long,
  remarkName: Option[String],
  realName: Option[String],
  birth: Option[Date],
  address: Option[String],
  phone: Option[String],
  qq: Option[String],
  email: Option[String])
我想用表单的
非空
字段更新数据库,我是这样做的

def updateField(form: CustomerUpdateForm) = {
  def updateField0[T](
    field: CustomerTable => Column[T],
    value: T) = {
     customerTable
      .filer(_.id, form.id)
      .map(c => field(c) -> c.gmtModified)
      .update(value -> new Date)
  }
  var updated = 0
  if (form.remarkName.nonEmpty)
    updateField0(_.remarkName, form.remarkName)
    updated = updated + 1
  if(form.realName.nonEmpty)
    updateField0(_.realName, form.realName)
    updated = updated + 1
  if(form.birth.nonEmpty)
    updateField0(_.birth, form.birth)
    updated = updated + 1
  if(form.address.nonEmpty)
    updateField0(_.address, form.address)
    updated = updated + 1
  if(form.phone.nonEmpty)
    updateField0(_.phone, form.phone)
    updated = updated + 1
  if(form.qq.nonEmpty)
    updateField0(_.qq, form.qq)
    updated = updated + 1
  if(form.email.nonEmpty)
    updateField0(_.email, form.email)
    updated = updated + 1
   if(updated  > 1) 1 else 0
}

我可以一次性更新这些字段吗

+1,我对此也很好奇。一种方法是从数据库中获取整行作为
CustomerTableRow
对象,然后使用反射检查表单字段是
Some
还是
None
,并相应地修改row对象,最后只需使用update函数即可。只是一个猜测,从来没有实际使用过反射,我不确定在这种情况下是否可能。CustomerTable是一个case类,对吗?是否在表单和自定义列对象中保留相同的名称?@dirceusemighini
CustomerTable
是一个
我有更新我的question@jilen我还是不明白你的问题。您不想使用表单来更新数据库值,或者不想使用从数据库中选择的值来创建表单?@dirceusemighini查看
updateField
方法。我想用表单中的非空字段更新我的数据库