Doctrine 2 LEFT JOIN不检索所有列

时间:2013-08-16 12:16:10

标签: sql symfony doctrine-orm

我在使用Symfony 2.3

的Doctrine Query时遇到了一个奇怪的问题

(提前,对不起我的查询中的英语/法语术语..:s)

在我的控制器中,当我在其中一个存储库上启动findAll()查询时,会生成以下查询:

SELECT t0.id AS id1, t0.date AS date2, t0.projet_id AS projet_id3, t0.societe_id AS societe_id4, t0.user_id AS user_id5 FROM Estimate t0

因此,我得到了所有列,包括涉及关系的列。 但是我需要在查询中添加leftJoin。我创建了一个自定义查询:

$qb = $this->createQueryBuilder('e')
    ->addSelect('e')
    ->leftJoin('e.works', 'w')
    ->addSelect('w')
    ->leftJoin('w.tache', 't')
    ->addSelect('t')
    ->leftJoin('t.groupetache', 'g')
    ->addSelect('g')
    ;

现在,我得到了所有已加入的列,但对于我的主表“Estimate”,查询只返回id和date(生成以下查询:)

SELECT e0_.id AS id0, e0_.date AS date1, w1_.id AS id4, w1_.description AS description5, w1_.prix_ha AS prix_ha6, w1_.prix_vente AS prix_vente7, t2_.id AS id8, t2_.nom AS nom9, t2_.prix_achat AS prix_achat10, g3_.id AS id11, g3_.nom AS nom12 FROM Estimate e0_ LEFT JOIN Work w1_ ON e0_.id = w1_.estimate_id LEFT JOIN Tache t2_ ON w1_.tache_id = t2_.id LEFT JOIN GroupeTaches g3_ ON t2_.groupetache_id = g3_.id

我错过了一点吗?

有一种方法可以获取所有主表字段,+正确连接字段吗?

我对Sequel Pro的“手动”查询返回了我想要的内容,我尝试使用DQL更改查询,如下所示:

$query = $this->getEntitymanager()
    ->createQuery('
        SELECT e0_.id AS id0, e0_.date AS date1, e0_.id AS id2, e0_.date AS date3,
        e0_.projet_id AS projet_id3, e0_.societe_id AS societe_id4, e0_.user_id AS user_id5,
        w1_.id AS id4, w1_.description AS description5, w1_.prix_ha AS prix_ha6, w1_.prix_vente AS prix_vente7,
        t2_.id AS id8, t2_.nom AS nom9, t2_.prix_achat AS prix_achat10, g3_.id AS id11, g3_.nom AS nom12
        FROM EcomCrmBundle:Estimate e0_
        LEFT JOIN EcomCrmBundle:Work w1_ WITH e0_.id = w1_.estimate_id
        LEFT JOIN EcomCrmBundle:Tache t2_ WITH w1_.tache_id = t2_.id
        LEFT JOIN EcomCrmBundle:GroupeTaches g3_ WITH t2_.groupetache_id = g3_.id');

但是我有一个错误,说Estimate没有projet_id字段:(

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我只是在猜测你用什么方法来检索你的结果? getArrayResult()? 尝试使用getResult()。它应该选择“估计”中的每一行。

相关问题