playframework 2.0 - Mapping many to one with Scala and Slick -
i have user can have 1 more more logininfos. wish find user id , pack each of associated logininfos user object.
my expectation number of rows equal number of logininfos each same user. first map user object , each of logininfos. there better way?
here's have:
def find(userid: uuid) = { val query = { dbuser <- slickusers.filter(_.id === userid.tostring) dbuserlogininfo <- slickuserlogininfos.filter(_.userid === dbuser.id) dblogininfo <- slicklogininfos.filter(_.id === dbuserlogininfo.logininfoid) } yield (dbuser, dblogininfo) db.run(query.result).map { results => var loginlist = list[logininfo]() results.foreach { case (user, logininfo) => loginlist = logininfo(logininfo.providerid, logininfo.providerkey) :: loginlist } results.headoption.map { case (user, logininfo) => user( uuid.fromstring(user.userid), loginlist, user.firstname, user.lastname, user.fullname, user.email, user.avatarurl) } }
i this:
def find(userid: uuid) = { val query = { dbuser <- slickusers.filter(_.id === userid.tostring) dbuserlogininfo <- slickuserlogininfos if(_.userid === dbuser.id) // join work dblogininfo <- slicklogininfos if(_.id === dbuserlogininfo.logininfoid) } yield (dbuser, dblogininfo) db.run(query.result).map { results => val grouped = results.groupby(_._1) // group user, max 1, it's ok.. grouped.map { case (user, group) => val logininfo = group.map{ case (dbuser, dblogininfo) => logininfo(dblogininfo.providerid, dblogininfo.providerkey) } user( uuid.fromstring(user.userid), logininfo, user.firstname, user.lastname, user.fullname, user.email, user.avatarurl) }.headoption } }
check out blog post, i've done similar. don't take granted, don't have table definitions, should work..
Comments
Post a Comment