JOIN DQL(symfony2)两个表之间的多个联接 [英] JOIN DQL (symfony2) multiple joins between two tables
问题描述
我的DQL查询有问题.我有一个表,其中包含3个外键userId,userRId和userAId.这些外键中的两个可能为NULL.
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 ...
推荐答案
在DQL中,您对对象(实体)而不是表(基于映射)进行操作.因此,当您拥有这样的实体时:
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
但是,要配合使用,您需要映射信息.如果没有映射,则可以使用doctrine:mapping:import生成它,只需在symfony项目中键入以阅读更多php app/console help doctrine:mapping:import
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
这篇关于JOIN DQL(symfony2)两个表之间的多个联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!