【问题标题】:How to make the Form mapping multi data-models in playframework?如何在 playframework 中制作表单映射多数据模型?
【发布时间】:2014-04-28 06:19:48
【问题描述】:

我使用 playframework 2.2+slick 2:

我的数据模型:

case class User(id:Option[String], firstName:String,secondName:String, addressid:Long)

class UserTable(tag: Tag) extends Table[User](tag, "User") {
def id = column[String]("ID", O.PrimaryKey)
def first_name = column[String]("First_Name")
def second_name=column[String]("Second_Name")
def addID = column[Long]("ADDRESS")
def home_address=foreignKey("ha_FK", addID, address)(_.id)
def * = (id.?, first_name, second_name, email, roles.?, profile,datebirth,addID)<>(User.tupled,User.unapply)

}
val user=TableQuery[UserTable]
}
case class Address(id:Long, state:String, city:String, street:String, unit: String, zip:String)
class AddressTable(tag:Tag) extends Table[Address](tag, "Address"){
def id = column[Long]("ID", O.PrimaryKey)
def state=column[String]("State")
def city=column[String]("City")
def street=column[String]("Street")
def unit=column[String]("Unit")
def zip=column[String]("zip")
def * = (id, unit, street, city, state, zip)<>(Address.tupled,Address.unapply)
}
val address = TableQuery[AddressTable]

我有一个表格来填写用户信息和他的地址信息。我不知道如何映射它们:(当然这里是假代码)

val fullForm = Form(
mapping(
  "User.first_name" -> text(),
  "User.second_name" -> text(),
  "User.id"-> ignored,
  "User.addID"->ignored,
  "Address.id"->ignored,
  "Address.state"->text,
  "Address.city"->text
  ...
)(User.apply, Address.apply)(User.unapply, Address.apply)
)

如何在此处将表单映射到多数据模型?你能举个例子吗?

【问题讨论】:

    标签: forms scala playframework-2.0 slick-2.0


    【解决方案1】:

    查看播放框架forms documentation 中的嵌套值部分。

    我不确定这是否有帮助,但类似

    val fullForm = Form(
        tuple(
            "User" -> mapping(
                "id"-> ignored(None),
                "first_name" -> text(),
                "second_name" -> text(),
                "addID"->ignored(0L))(User.apply)(User.unapply),
            "Address" -> mapping(
                "id"->ignored(0L),
                "state"->text,
                "city"->text 
                //...
                )(Address.apply)(Address.unapply))
    

    将为您提供一个包含地址和用户的表单。然后你必须以某种方式将它们绑定在一起。

    【讨论】:

      猜你喜欢
      • 2014-05-23
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      相关资源
      最近更新 更多