我无法弄清楚如何计算Slick(3)。
val posts = for {
p <- Posts.query if p.receiver === userId
comments <- Comments.query if comments.postId === p.id
author <- Users.query if p.author === author.id
receiver <- Users.query if p.receiver === receiver.id
} yield (p, comments, author, receiver)
具有以下关系
Posts : Author : Receiver : Comments
1 : 1 : 1 : N
结果应为:
Future[Seq[(Post, User, User, Int)]]
int
是评论次数grouped by Posts
任何提示?
答案 0 :(得分:3)
您需要按照帖子,作者和接收者以及地图对结果进行分组,以便仅通过计算来汇总评论。
val posts = (for {
p <- Posts.query if p.receiver === userId
comment <- Comments.query if comments.postId === p.id
author <- Users.query if p.author === author.id
receiver <- Users.query if p.receiver === receiver.id
} yield (p, comment, author, receiver)) //So far thats your current query
.groupBy({ //Group by post, author and receiver
case (post, comment, author, receiver) =>
(post, author, receiver)
})
.map({ //Aggregate your comments (second argument in tuple) and count them
case ((post, author, receiver), list) => {
(post, author, receiver, list.map(_._2).count))
}
})
目前在移动设备上,所以这可能无法编译,但你应该明白这一点。