I get the form from the Play controller
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])
And I want to update the database with nonEmpty form fields, this is how I did
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 }
Can I update these fields at a time?
jilen source share