1

解决方案:我不知道我们如何返回 Option[User] 的不存在,所以在未找到用户的情况下,我创建了一个虚拟用户对象并从控制器对其进行推理(感觉很糟糕但工作......) : 来自 Application.scala

val loginForm = Form(
tuple(
  "email" -> text,
  "password" -> text
) verifying ("Invalid email or password", result => result match {
  case (email, password)  => (User.authenticate(email, password).map{_.id}.getOrElse(0) != 0)
})

)

反对:

val loginForm = Form(
  tuple(
  "email" -> text,
  "password" -> text
) verifying ("Invalid email or password", result => result match {
  case (email, password) => User.authenticate(email, password).isDefined
})

)

++++++++++++++++++ 原版 2 ++++++++++++++++++++ 感谢您的建议!我做了一些改变,似乎越来越近了,但是我不知道如何返回一个未定义的选项 [用户]。我也试过 case _ => null,见下文:

来自 User.scala

case class User(id: Int, email: String, name: String, password: String)

object User {

  // -- Parsers

  /**
   * Parse a User from a ResultSet
   */
  val userParser = {
            get[Option[Int]]("uid")~        
            get[Option[String]]("email")~
            get[Option[String]]("fname")~
            get[Option[String]]("pbkval") map {
            case (uid~email~name~pbkval) => validate(uid,email, name, pbkval)
            }
  }

  /**
   * Retrieve a User from email.
   */
  def findByEmail(email: String): Option[User] = {
    DB.withConnection { implicit connection =>
      SQL("select * from get_pbkval({email})").on(
                'email -> email         
            ).as(userParser.singleOpt)
    }
  }


  /**
   * Authenticated user session start.
   */
  def authenticate(email: String, password: String): Option[User] = {
    DB.withConnection { implicit connection =>
      SQL(
            """
                        select * from get_pbkval({email})
            """
            ).on(
                'email -> email
            ).as(userParser.singleOpt)

        }
    }

  /**
   * Validate entry and create user object.
   */
    def validate(uid: Option[Int], email: Option[String], fname: Option[String], pbkval: Option[String]): User = {
                val uidInt : Int = uid.getOrElse(0)
                val emailString: String = email.getOrElse(null)
                val fnameString: String = fname.getOrElse(null)
                val pbkvalString: String = pbkval.getOrElse(null)
                User(uidInt, emailString, fnameString, pbkvalString)
    }

我想很明显,我在这里并没有真正得到一些基本的东西。我已经阅读了http://www.playframework.org/modules/scala-0.9.1/anorm并搜索了几个小时。任何帮助都是非常感激!

4

1 回答 1

3

您没有指定要映射的行。在行映射器之后,放一个 * 来表示要映射的行。当您使用它时,我发现在单独的 val 中定义我的行映射器更容易。像这样的东西。

 val user = { 
   get[Option[Int]]("uid")~        
   get[Option[String]]("email")~
   get[Option[String]]("fname")~
   get[Option[String]]("pbkval") map {
    case uid~email~name~password => validate(uid,email, name, password)
  }
}

def authenticate(email: String, password: String): Option[User] = {
DB.withConnection { implicit connection =>
  SQL(
    """
        select * from get_pbkval({email})
    """
    ).on(
        'email -> email
    ).as(user *)
  }
 } 

    def validate(uid: Option[Int], email: Option[String], fname: Option[String], pbkval: Option[String]): Option[User] = {
    if (uid != None) {
            val uidInt : Int = uid.getOrElse(0)
            val emailString: String = email.getOrElse(null)
            val fnameString: String = fname.getOrElse(null)
            val pbkvalString: String = pbkval.getOrElse(null)
            User(uidInt, emailString, fnameString, pbkvalString)
    } else { return null}
}

请注意,“as”方法现在有两个参数,您的行映射器(现在定义为 val“用户”和一个“*”,表示您要映射所有行。

于 2012-07-30T15:42:10.437 回答