小编典典

Play2和Anorm,如何使一对多关系成为“众人皆知”

sql

我与User和LinkedAccount之间存在一对多关系,一个User可以有多个链接帐户。

LinkedAccount(id, provider_user_id, salt, provider_id, auth_method, avatar_url, User.findBy(user)) 在解析器中加载LinkedAccount及其用户是没有问题的。

我不知道的是如何用LinkedAccounts加载用户。我想我需要让用户知道LinkedAccounts ..但是如何?

每当我想查找用户是否具有给定类型的链接帐户时,我都希望摆脱对数据库的一个额外的sql调用。目前,我确实喜欢这样:

def findLinkedAccountByUserAndProvider(userId: Pk[Long], providerId : String) = {
    DB.withConnection {
      implicit connection =>
        SQL("select * from linked_account la where la.user_id = {userId} and la.provider_id = {providerId}")
          .on("userId" -> userId, "providerId" -> providerId).as(LinkedAccount.simple.singleOpt)
    }
  }

还是当用户知道它的LinkedAccounts和LinkedAccount知道它的用户时,这会引起问题吗?

用户:

case class User(id: Pk[Long] = NotAssigned,
                firstName: String,
                lastName: String,
                email: String,
                emailValidated: Boolean,
                lastLogin: DateTime,
                created: DateTime,
                modified: DateTime,
                active: Boolean)

object User {
  val simple = {
    get[Pk[Long]]("id") ~
      get[String]("first_name") ~
      get[String]("last_name") ~
      get[String]("email") ~
      get[Boolean]("email_validated") ~
      get[DateTime]("last_login") ~
      get[DateTime]("created") ~
      get[DateTime]("modified") ~
      get[Boolean]("active") map {
      case id ~ first_name ~ last_name ~ email ~ email_validated ~ last_login ~ created ~ modified ~ active =>
        User(id, first_name, last_name, email, email_validated, last_login, created, modified, active)
    }
  }
}

关联帐户:

case class LinkedAccount(id: Pk[Long] = NotAssigned,
                         providerUserId: String,
                         salt: Option[String],
                         providerId: String,
                         authMethod: Option[String],
                         avatarUrl: Option[String],
                         user: User
                         )

object LinkedAccount {
  val simple = {
    get[Pk[Long]]("id") ~
    get[String]("provider_user_id") ~
    get[Option[String]]("salt") ~
    get[String]("provider_id") ~
    get[Option[String]]("auth_method") ~
    get[Option[String]]("avatar_url") ~
    get[Pk[Long]]("user_id") map {
      case id ~ provider_user_id ~ salt ~ provider_id ~ auth_method ~ avatar_url ~ user  =>
        LinkedAccount(id, provider_user_id, salt, provider_id, auth_method, avatar_url, User.findBy(user))
    }
  }
}

阅读 201

收藏
2021-04-28

共1个答案

小编典典

由于我没有答案,因此我将提供自己的答案:

在案例类中,我定义:

case class User(id: Pk[Long] = NotAssigned,
                firstName: String,
                lastName: String,
                email: String,
                emailValidated: Boolean,
                lastLogin: DateTime,
                created: DateTime,
                modified: DateTime,
                active: Boolean) {

lazy val linkedAccounts: Seq[LinkedAccount] = DB.withConnection("test") { implicit connection =>
    SQL(
      """
        select * from linked_account la
        join users on la.user_id = users.id
        where la.id = {id}
      """
    ).on(
      'id -> id
    ).as(LinkedAccount.simple *)
  }
}

然后通过以下方式获取帐户:

val linkedAccounts = user.linkedAccounts

在这里找到这个

2021-04-28