1

我不知道如何用 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

有小费吗?

4

1 回答 1

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))
      }
  })

目前在移动设备上,所以这可能无法编译,但你应该明白了。

于 2015-08-06T11:02:34.603 回答