简体   繁体   English

如何在doctrine1.2中使用innerJoin?

[英]How to use innerJoin with doctrine1.2?

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... :-( Doctrine的文档仅包含基本示例... :-(

  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)? 我可以对innerJoin()任何表(例如与Users相关的用户类型)使用innerJoin()还是仅对与UsersProjects相关的表(在这种情况下:Projects和Users)使用innerJoin()? When I trying to do it then I get error "Unknown relation". 当我尝试执行此操作时,出现错误“未知关系”。

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). 主义查询使用“流利”接口,这意味着每个方法都返回对查询的引用,以便您可以保持链接新方法(select(),innerJoin(),from(),where()等)的链接。 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). 您可以根据需要添加任意数目的innerJoins,但是联接的对象/表需要与已经联接的对象/表之一(或表的基础)相关。 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. 如果不进入该学说所提供的RawSql接口,就无法联接不相关的表。 You can see that only Users relates to the base table UsersProjects. 您可以看到只有用户与基表UsersProjects相关。 PhoneNumbers and Addresses relate to a User and City relates to an Address. 电话号码和地址与用户有关,城市与地址有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM