1

I use playframework2+slick2.0

My datamodel:

class Page(tag:Tag) extends Table[(Int,Int, String,String,String, Option[String], Option[String])](tag, "Page"){
  def id=column[Int]("ID", O.PrimaryKey)
  def subId=column[Int]("subject")
  def title=column[String]("Title", O.NotNull)
  def describe=column[String]("Describe")
  def profile=column[String]("Profile")
  def icon=column[Option[String]]("icon")
  def resId=column[String]("Picture")
  def * = (id, subId,title, describe, profile,icon, resId.?)
  def page_sub=foreignKey("PA_SU_FK", subId, subject)(_.id)
  def page_res=foreignKey("PA_RE_FK", resId, resource)(_.link)

}
    val page=TableQuery[Page]

class Resource(tag:Tag) extends Table[(String, String, Boolean)](tag, "Resource"){
  def link=column[String]("Link", O.PrimaryKey)
  def rtype=column[String]("class")
  def local=column[Boolean]("local")
  def * = (link, rtype,local)
}
val resource=TableQuery[Resource]

I want to use the filter to get one row, and get the resource object form page:

my code is like:

 val item=page.filter(_.id===id.toInt).first()(rs.dbSession)

This is only get the page real object, I want get the correspond resource object, how to get it by filter?

user504909
  • 9,119
  • 12
  • 60
  • 109

1 Answers1

1

You can use for-comprehension:

 val pageWithResouce = (for {
   p <- page if (p.id === id.toInt)
   r <- resource if (p.resId === r.link)
  } yield { (p, r) })list
Sky
  • 2,509
  • 1
  • 19
  • 28