子查询在Doctrine 2中

时间:2019-01-17 15:17:10

标签: sql doctrine-orm doctrine

我无法使子查询与Doctrine一起使用。

这是我的要求:

$qb = $this->getEntityManager()->createQueryBuilder()
            ->select("plafond")
            ->addSelect("plafond.id AS id")
            ->addSelect('(select count(gd) from ACCUEILBundle:Ttgeneration gd 
                inner join ACCUEILBundle:Tgeneration gn with gn.id = gd.generation 
                where gn.nouveaulot = :new_generation and 
                gd.donnee = :entity_id and gd.action = :action_creation) as suppr')
            ->from("ACCUEILBundle:Trprst", "plafond")
            ->setParameter('new_generation', 1)
            ->setParameter('entity_id', 'plafond.id')
            ->setParameter('action_creation',32);

我想问题出在带plafond.id的setParameter中:好像没有使用当前记录的ID。

实际上,对于每条记录,此suppr字段应为0,但应为39。但是对于主义查询,就像我对每条记录都有0。

但是当我用纯sql编写查询时,没问题:

SELECT plafond.id AS id, 
  (select count(*) from ttgeneration gd
     inner join tgeneration gn on gn.id = gd.generation
   where gn.nouveaulot = 1 and
     gd.donnee = plafond.id and gd.action = 32) as suppr
FROM trprst plafond

纯sql查询的结果:

36      0                     
37      0                     
38      0                     
39      1  

谢谢

0 个答案:

没有答案
相关问题