【问题标题】:I want mapping the datamodel to a form how to convert the data type?我想将数据模型映射到表单如何转换数据类型?
【发布时间】:2014-04-07 23:22:13
【问题描述】:

我使用 slick2 和 playframework2。

我想将我的模型映射到来自

我的模型是:

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]
}

我想把它映射到一个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)
)
}

错误是:

[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

这个映射方法怎么写? slick2 和 playframework2 是否有数据映射到 Form 的示例?

【问题讨论】:

    标签: scala playframework slick slick-2.0


    【解决方案1】:

    如您所见,Form 要求您提供Mapping,所以它应该是"id" -&gt; number,而不是"id" -&gt; Int

    UPD:您的Subjectsub_resource 定义为String,但您的映射显示它是optional(text),这意味着Option[String]

    您应该将您的 Subject 模型更改为 case class Subject(id: Int, name:String, describe: String, sub_resource:Option[String].... 或将您的映射替换为 "sub_resource" -&gt; text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2012-02-25
      相关资源
      最近更新 更多