문제

I use playframework 2.2+slick 2:

my datamodel:

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]

I have a form to fill both user information and his address information. I do not know how to mapping them:(of course here is fake code)

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

How to mapping a Form to multi-data-models here? Can you give this case an example?

도움이 되었습니까?

해결책

Have a look at nested values section in the play framework forms documentation.

I am not sure if this helps but something like

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

will give you a form for a pair of an address and a user. You will then have to somehow bind them together.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top