如何在doctrine1.2中使用innerJoin?

发布于 2024-10-09 01:40:02 字数 366 浏览 2 评论 0原文

我有一些代码(有效):

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

两个问题:

  1. 有人可以给我举一个例子,如何加入下一张表(多个表)? Doctrine 的文档仅包含基本示例... :-(

  2. 可以我将 innerJoin() 与我的数据库中的任何表(例如与用户相关的用户类型)或仅与与 UsersProjects 相关的表(在本例中:项目和用户)一起使用?我收到错误“未知关系”。

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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-10-16 01:40:02

Doctrine 查询使用“流畅”接口,这意味着每个方法都会返回对查询的引用,以便您可以继续链接新方法(select()、innerJoin()、from()、where() 等)。您可以根据需要添加任意数量的innerJoins,但连接的对象/表需要与您已连接的对象/表之一相关(或表中的基础)。例如:

$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

如果不进入原则提供的RawSql接口,就无法连接不相关的表。您可以看到只有 Users 与基表 UsersProjects 相关。电话号码和地址与用户相关,城市与地址相关。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文