doufangzhang4454 2010-12-24 15:03
浏览 7

如何使用内部加入与学说1.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... :-(

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

  • 写回答

1条回答 默认 最新

  • duanqun9740 2011-06-03 18:58
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系