OneToMany elevator relationship

I have a question again, and I feel that the solution is simple. Should be simple, anyway. I am trying to simulate my mappings for my database, with a nice feature OneToMany, like

class User extends MegaProtoUser[User] with OneToMany[Long, User] {
  def getSingleton = User

  object posts extends MappedOneToMany(Post, Post.author, OrderBy(Post.edited, Descending))
}

class Post extends LongKeyedMapper[Post] with IdPK with OneToMany[Long, Post] {
  def getSingleton = Post

  object author extends LongMappedMapper(this, User)

  object title extends MappedString(this, 100) {
    override def dbIndexed_? = true

    override def defaultValue = "New Post"
  }

  object contents extends MappedText(this)

  object edited extends MappedDate(this)
}

Everything is well done, I have a one-to-many relationship. But, what bothers me a lot, how can I get data in these respects? Suppose I have an object Postand you want to get its author name?

bind("post", in, "title" -> post.title, "author" -> /* insert overly complicated method here */)

I found out that I can get it through Box, post.author.objor something like that. It doesn’t quite work out, should I do it matchover him and so?

I feel the answer is simple, but I do not see it.

+3
2

Mapper , post.author , Box. post.author.obj.map(_.name.is) openOr "unknown name" , match.

Post, .

+4
post.author.obj.map(_.userNameOrSomething).openOr("Unknown writer")
+3

Source: https://habr.com/ru/post/1781366/


All Articles