21

For joining between two tables is done like

    (for {
    (computer, company) <- Computers leftJoin Companies on (_.companyId === _.id)
    if computer.name.toLowerCase like filter.toLowerCase()
    }

But in case if joining required between more tables what is the right way trying below but doesnt work

   (for {
    (computer, company,suppliers) <- Computers leftJoin Companies on (_.companyId ===        _.id)
     //not right leftjoin Suppliers on (_.suppId === _.id)
    if computer.name.toLowerCase like filter.toLowerCase()
  }
dsr301
  • 759
  • 3
  • 7
  • 21

2 Answers2

36

The first join results in a Query returning Tuples. One of the tuple components has the foreign key you want to use for the second join. You need to get this component in the second join condition before getting its field. If Companies is the table, that has the field suppId it would look like this:

(for {
  ((computer, company),suppliers) <- Computers leftJoin Companies on (_.companyId === _.id) leftJoin Suppliers on (_._2.suppId === _.id)
  if computer.name.toLowerCase like filter.toLowerCase()
} yield ... )
cvogt
  • 11,260
  • 30
  • 46
  • here leftJoin Suppliers on (_._2.suppId === _.id) is this join on the first tuple? in case if want to join between computer and suppliers i am trying with leftJoin Suppliers on (_._1.suppId === _.id) but this one fails – dsr301 Sep 04 '13 at 08:10
  • This works, but i see that the full query with joins should be in one line , else its showing compilation errors – dsr301 Sep 04 '13 at 09:39
  • changed the answer to be in one line – cvogt Sep 04 '13 at 12:29
4

I think you want this:

for {
    (computer, company) <- Computers leftJoin Companies on (_.companyId === _.id)
    (supp, _) <- company innerJoin Suppliers on (_.suppId === _.id)
    if computer.name.toLowerCase like filter.toLowerCase()
} yield (computer, company, supp)

Of course, I am making assumptions about your model. Not sure if suppliers are linked with companies or computers.

pedrofurla
  • 12,763
  • 1
  • 38
  • 49