Question

I use slick2 and playframework2.

I want mapping my model to a from

My model is:

object Web extends App{
    case class Subject(id: Int, name:String, describe: String, sub_resource:String, addId:Long, recommand:Int, commentsum :Int, commentnumber: Int, userId: Int)
class Subjects(tag: Tag) extends Table[Subject](tag, "Subject") {
  def id=column[Int]("ID", O.PrimaryKey)
  def name=column[String]("Name")
  def describe=column[String]("describe")
  def sub_resource=column[String]("Resource")
  def keywords=column[String]("Keyword")
  def addID=column[Long]("Address")
  def recommandrate=column[Int]("Recommand")
  def commentsum=column[Int]("Sum_of_rate")
  def commentnumber=column[Int]("Rate_number")
  def userId=column[Int]("owner")
  def uniqueName = index("idx_grp_name", name, unique = true)
  def * = (id, name,sub_resource,keywords, addID, recommandrate, commentsum, commentnumber,userId)<> (Subject.tupled, Subject.unapply)
  def sub_res=foreignKey("sub_res_FK", sub_resource, resource)(_.link)
  def sub_address=foreignKey("sub_add_FK", addID, address)(_.id)
  def sub_user=foreignKey("sub_user_FK", userId, user)(_.id)
}
val subject = TableQuery[Subject]
}

I want mapping it to a dataForm:

object ShoplistController extends Controller {
val shopForm = Form(
mapping(
  "id" -> number,
  "name" -> nonEmptyText,
  "describe"->nonEmptyText,
  "sub_resource" -> optional(text),

  "addId"->longNumber, 
  "recommand"->number, 
  "commentsum"->number, 
  "commentnumber"-> number,
  "userId"->number

)((Subject.apply _).tupled)(Subject.unapply)
)
}

the error is:

[info] Compiling 2 Scala sources to C:\testprojects\slickplay\target\scala-2.10\
classes...
[error] C:\testprojects\slickplay\app\controllers\ShoplistController.scala:23: t
ype mismatch;
[error]  found   : (Int, String, String, String, Long, Int, Int, Int, Int) => mo
dels.Web.Subject
[error]  required: (Int, String, String, Option[String], Long, Int, Int, Int, In
t) => ?
[error]     )(Subject.apply )(Subject.unapply)
[error]               ^
[error] one error found
[error] (compile:compile) Compilation failed

How to write this mapping method? and is there any example for data mapping to Form for slick2 and playframework2?

Was it helpful?

Solution

As you can see the Form asks you to provide a Mapping, so it should be "id" -> number, and not a "id" -> Int

UPD: your Subject's sub_resource is defined as String, but your mapping says it is optional(text) which means an Option[String].

You should either change your Subject model to case class Subject(id: Int, name:String, describe: String, sub_resource:Option[String].... or replace your mapping with "sub_resource" -> text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top