在剧中!框架,使用scala,说我有一个如下形式:
import play.api.data._
import play.api.data.Forms._
import play.api.data.validation.Constraints._
case class User(someStringField: String, someIntField: Int)
val userForm = Form(
mapping(
"someStringField" -> text,
"someIntField" -> number verifying(x => SomeMethodThatReceivesAnIntAndReturnsABoolean(x))
)(User.apply)(User.unapply)
Run Code Online (Sandbox Code Playgroud)
)
where SomeMethodThatReceivesAnIntAndReturnsABoolean是一个在int上执行某些逻辑以验证它的方法.
但是,我希望能够考虑someStringField验证时的价值someIntField,有没有办法在Play框架的形式中实现这一点?我知道我可以这样做:
val userForm = Form(
mapping(
"someStringField" -> text,
"someIntField" -> number
)(User.apply)(User.unapply)
.verifying(x => SomeFunctionThatReceivesAnUserAndReturnsABoolean(x))
Run Code Online (Sandbox Code Playgroud)
然后我会将整个用户实例传递给验证函数.该方法的问题在于,产生的错误将与整个表单相关联,而不是与someIntField字段相关联.
有没有办法获得两件事,使用另一个字段验证字段并维护与我希望验证的特定字段相关的错误,而不是整个表单?
说我有这样的表:
UserActions
UserId INT
ActionDate TIMESTAMP
Description TEXT
Run Code Online (Sandbox Code Playgroud)
包含用户执行某些行为的日期.如果我想获得每个用户执行的最后一个操作,我将不得不在SQL中执行以下操作:
SELECT *
FROM UserActions,
(
SELECT ua.UserId,
max(ua.ActionDate) AS lastActionDate
FROM UserActions ua
GROUP BY ua.UserId
) AS lastActionDateWithUserId
WHERE UserActions.UserId = lastActionDateWithUserId.UserId
AND UserActions.ActionDate = lastActionDateWithUserId.lastActionDate
Run Code Online (Sandbox Code Playgroud)
现在,假设我已经在scalaquery 0.9.5中为UserActions设置了表结构,例如:
case class UserAction(userId:Int,actionDate:Timestamp,description:String)
object UserActions extends BasicTable[UserAction]("UserActions"){
def userId = column[Int]("UserId")
def actionDate = column[Timestamp]("ActionDate")
def description = column[String]("Description")
def * = userId ~ actionDate ~ description <> (UserAction, UserAction.unapply _)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在ScalaQuery/SLICK中我该如何执行这样的查询?