【发布时间】:2014-03-02 16:29:44
【问题描述】:
使用findBy 如下所示,我可以在Authentications 表中搜索key 和value 的记录
def findBy(key: String, value: String): Any = DB.withConnection { implicit connection =>
SQL("select * from authentications where {key}={value} limit 1").on('key -> key, 'value -> value)
}
例子:
Authentication.findByMe("email", "toot@toot.com")
返回:
res1: Any = SimpleSql(SqlQuery(select * from authentications where ?=? limit 1,List(key, value),None),List((key,ParameterValue(email,anorm.ToStatement$$anon$3@4ca9aed5)), (value,ParameterValue(toot@toot.com,anorm.ToStatement$$anon$3@18154945))),<function1>)
如何将其转回身份验证对象?一个包含我从查询中返回的所有字段的对象。
我尝试了以下方法,但是当其中一列中有空值时,我无法传递可空对象错误。我不知道如何使用提供的解决方案here
val authentication_parser =
get[String]("email") ~
get[String]("encrypted_password") ~
get[String]("user_id") ~
get[String]("token") ~
get[Date]("created_at") ~
get[Date]("updated_at")
val authentication = {
authentication_parser map {
case email~encrypted_password~user_id~token~created_at~updated_at => Authentication(email, encrypted_password, user_id, token, created_at, updated_at)
}
}
像这样使用它:
def findBy(key: String, value: String): Any = DB.withConnection { implicit connection =>
SQL("select * from authentications where {key}={value} limit 1").on('key -> key, 'value -> value).as(authentication *)
}
我也是 Scala 的新手,所以我经常遇到这样的问题。
【问题讨论】:
标签: scala playframework-2.0 anorm