我使用 scalikejdbc 2.0.1 和 playframework 2.3。我按照一对多http://scalikejdbc.org/documentation/one-to-x.html的说明进行操作,但还是报错:
我的数据模型是,一个帐户有许多待办事项:
待办模型:</p>
case class Todo (id: Long, value: String, userId:Option[Long] = None, users: Option[Account] = None){}
object Todo extends SQLSyntaxSupport[Todo]{
val todo = syntax("todo")
override val tableName = "todo"
private val auto = AutoSession
def opt(m: ResultName[Todo])(rs: WrappedResultSet) = rs.longOpt(m.id).map(_ => Todo(m)(rs))
apply(todo.resultName)(rs)
def apply(a: ResultName[Todo])(rs: WrappedResultSet): Todo = new Todo(
id = rs.long(todo.id),
userId =rs.longOpt(todo.userId),
value = rs.string(todo.value)
)
def apply(m: ResultName[Todo], a: ResultName[Account])(rs: WrappedResultSet) = {
apply(m)(rs).copy(users = rs.longOpt(a.id).map(_ => Account(a)(rs)))
}
}
模型帐户是:
case class Account(id: Int, email: String, password: String, name: String, permission: Role,todos:Seq[Todo]=Nil)
object Account extends SQLSyntaxSupport[Account] {
...
val (a, t) = (Account.syntax, Todo.syntax)
val accounts: List[Account] = withSQL {
select.from(Account as a).leftJoin(Todo as t).on(a.id,t.userId)
}.one(Account(a))
.toMany(Todo.opt(t))
.map { (account, todos) => account.copy( todos = todos) }
.list.apply()
}
}
我得到的错误是:
[error] G:\testprojects\mifun\app\models\Todo.scala:23: overloaded method apply
needs result type
[error] apply(m)(rs).copy(users = rs.longOpt(a.id).map(_ => Account(a)(rs)))
[error] ^
[error] G:\testprojects\mifun\app\models\Account.scala:53: type mismatch;
[error] found : scalikejdbc.QuerySQLSyntaxProvider[scalikejdbc.SQLSyntaxSuppo
rt[models.Todo],models.Todo]
[error] required: scalikejdbc.ResultName[models.Todo]
[error] (which expands to) scalikejdbc.ResultNameSQLSyntaxProvider[scalikej
dbc.SQLSyntaxSupport[models.Todo],models.Todo]
[error] .toMany(Todo.opt(t))
[error] ^
[error] two errors found
[error] (compile:compile) Compilation failed
我有两个问题:
1、为什么我不能用toMany?我想用ResultNameSQLSyntaxProvider,我写的opt函数怎么改?
2、在 Todo.scala:23 上应该给出什么 rs 类型?