简体   繁体   English

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

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

I want to combine the form binding from play 2.0 framework with a class extending Record from the circumflex-orm ( website ). 我想将play 2.0框架中的表单绑定与来自circumflex-orm( 网站 )的扩展Record的类结合起来。

These are my class objects: 这些是我的类对象:

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
}

And this is what i try to do with the play form: 这就是我尝试使用游戏形式:

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

But i get an error like this: 但我得到这样的错误:

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

And if i replace Option by Some: 如果我用一些替换Option:

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

I am clueless right now and any hint would be appreciated. 我现在很无能,任何提示都会受到赞赏。

Thanks a lot. 非常感谢。

EDIT: I made a basic error, i did name the Form: 编辑:我做了一个基本的错误,我确实命名了表格:

val taskForm: Form[Tasks] = Form(

when the name of the class is "Task". 当类的名称是“任务”时。 So i can change it to: 所以我可以改为:

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

And now i get a different error: 现在我得到一个不同的错误:

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

I made a simple project with the needed dependencies in eclipse, you can download it here and look at it, if it helps: Basic Form Example 我在eclipse中创建了一个带有所需依赖项的简单项目,你可以在这里下载并查看它,如果它有帮助: 基本表单示例

I was wrong in comment, following snippet works for me. 我的评论错了,以下片段为我工作。

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

Update 更新

I added circumflex to dependencies and tried your exact example. 我将circumflex添加到依赖项并尝试了您的确切示例。 It compiles fine for me, I just added 我刚刚补充说,它对我来说很好

object Task extends Task with Table[Long, Task]

I believe you forget to include it in the question. 我相信你忘了将它包括在问题中。 So I can only suggest to clean and rebuild entire project. 所以我只能建议清理和重建整个项目。

PS and I changed line PS和我换线了

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

but it is obvious. 但很明显。

The main problem is if you use circumflex you don't write case classes so you don't have apply and unapply methods by default. 主要问题是如果使用circumflex,则不编写case类,因此默认情况下没有apply和unapply方法。

You have to write your own apply and unapply methods in your Task companion object like this: 您必须在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