2

我在用户和 LinkedAccount 之间有一对多的关系,一个用户可以有多个关联的帐户。

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

1 回答 1

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

在这里找到这个

于 2013-03-22T09:58:55.390 回答