0

I have some code (works):

$q = Doctrine_Query::create()
     ->from('UsersProjects up')
     ->innerJoin('up.Users u');

Two questions:

  1. Could sombody show me an example, how to join next table (more then one)? Doctrine's documentation contains only basic examples... :-(

  2. Can I use innerJoin() with any table from my db (eg. Usertypes related with Users) or only with table related with UsersProjects (in this case: Projects and Users)? When I trying to do it then I get error "Unknown relation".

j0k
  • 22,600
  • 28
  • 79
  • 90
peter
  • 71
  • 1
  • 5

1 Answers1

4

Doctrine queries use a "fluent" interface, which means each method returns a reference to the query, so that you can keep chaining new methods (select(), innerJoin(), from(), where(), etc). You can add as many innerJoins as you want, but the joined object/table needs to be related to one of the ones you have already joined (or the base from table). For example:

$q = Doctrine_Query::create() 
  ->from('UsersProjects up') 
  ->innerJoin('up.Users u')
  ->innerJoin('u.PhoneNumbers p') // users may have multiple phone numbers
  ->innerJoin('u.Addresses a') // users may have multiple addresses
  ->innerJoin('a.City c'); // each address has a city

You can't join unrelated tables without getting into the RawSql interface that doctrine provides. You can see that only Users relates to the base table UsersProjects. PhoneNumbers and Addresses relate to a User and City relates to an Address.

dbellizzi
  • 713
  • 8
  • 18