繁体   English   中英

使用scala和circumflex-orm类在play框架2中提供自己的unapply方法

[英]provide own unapply method in play framework 2 with scala and circumflex-orm class

我想将play 2.0框架中的表单绑定与来自circumflex-orm( 网站 )的扩展Record的类结合起来。

这些是我的类对象:

class Task extends Record[Long, Task] with IdentityGenerator[Long, Task] {  
  def this(name: String, description: String) = {
    this()
    this.name := name
    this.description := description
}

  val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
  val name = "name".VARCHAR(255).NOT_NULL
  val description = "description".TEXT.NOT_NULL

  def PRIMARY_KEY = id
  def relation = Task
}

这就是我尝试使用游戏形式:

val taskForm: Form[Tasks] = Form(
  mapping(
    "name" -> text,
    "description" -> text
  )
  {(name, description) => Task(name, description)}
  {(t: Task) => Option(t.name(), t.description())  }
)

但我得到这样的错误:

found   : models.Task => Option[(String, String)]
required: Unit => Option[(String, String)]
  {(t: Task) => Option(t.name(), t.description())}

如果我用一些替换Option:

found   : models.Task => Some[(String, String)]
required: Unit => Option[(String, String)]
  {(t: Task) => Some(t.name(), t.description())}

我现在很无能,任何提示都会受到赞赏。

非常感谢。

编辑:我做了一个基本的错误,我确实命名了表格:

val taskForm: Form[Tasks] = Form(

当类的名称是“任务”时。 所以我可以改为:

val taskForm: Form[Task] = Form(
  mapping(
      "name" -> text,
      "description" -> text
  ) ( (name, description) => Task ) 
  ( (t: Task) => Option() )
)

现在我得到一个不同的错误:

Unspecified value parameter x
  ( (t: Task) => Option() )

我在eclipse中创建了一个带有所需依赖项的简单项目,你可以在这里下载并查看它,如果它有帮助: 基本表单示例

我的评论错了,以下片段为我工作。

case class Foo(x: String, y: String)

val taskForm = Form(
  mapping(
    "name" -> text,
    "description" -> text)
  ((name, description) => Foo(name, description))
  ((t: Foo) => Some(t.x, t.y)))

更新

我将circumflex添加到依赖项并尝试了您的确切示例。 我刚刚补充说,它对我来说很好

object Task extends Task with Table[Long, Task]

我相信你忘了将它包括在问题中。 所以我只能建议清理和重建整个项目。

PS和我换线了

{ (name, description) => new Task(name, description) }

但很明显。

主要问题是如果使用circumflex,则不编写case类,因此默认情况下没有apply和unapply方法。

您必须在Task伴随对象中编写自己的apply和unapply方法,如下所示:

    object Taks extends Task with Table[Long, Task] {
        def apply(name:String,description:String) = {
            var t = new Task()
            t.name := name
            t.description := description
            t
        }
        def unapply(t:Task) = Some(t.name(),t.description())
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM