JOIN DQL (symfony2) multiple joins between two tables

拈花ヽ惹草 提交于 2020-01-06 14:50:11

问题


I have a problem in my DQL query. I have a table which has 3 foreign keys userId, userRId and userAId. Two of those foreign keys may be NULL.

I want to join all the foreign keys in the query but i don't know how to join two or three foreign keys between the same tables. (see query ) Could somebody give me some ideas??

   **TABLE A**
   id    userId   userRId   userAId
   1     2        NULL      NULL
   1     2         1        NULL
   1     2        NULL         1

**TABLE USER**
userId  name
  2     xxxx
  1     xxxx

The DQL query:

"SELECT FROM nasyBundle:A a JOIN a.userId u , a JOIN userRId , a JOIN userAid
         WHERE ...

回答1:


In DQL you operate on objects(entities) not tables (based on mappings). So when you have entities like this:

class User
{
    private $id;
    private $name;
}

class TableA
{
    private $id;
    private $user;
    private $userR;
    private $userA;
}

you can create query like this (when you have valid mappings)

SELECT a
FROM nastyBundle:TableA a
INNER JOIN a.user u
LEFT JOIN a.userR ur
LEFT JOIN a.userA ua

But yeah to work with that you need mapping information. If you do not have mappings you can generate it using doctrine:mapping:import just type in you symfony project to read more php app/console help doctrine:mapping:import



来源:https://stackoverflow.com/questions/11484945/join-dql-symfony2-multiple-joins-between-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!