光滑的动态更新字段

时间:2014-06-18 10:28:23

标签: 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: Long,
  remarkName: Option[String],
  realName: Option[String],
  birth: Option[Date],
  address: Option[String],
  phone: Option[String],
  qq: Option[String],
  email: Option[String])

我希望使用表单的nonEmpty字段更新数据库,这是我的工作方式

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
}

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

0 个答案:

没有答案