Doctrine querybuilder

时间:2015-05-07 11:14:13

标签: symfony doctrine-orm query-builder

我构建了一个symfony项目,但是我对doctrine查询构建器有一个问题。 我有两个实体,一个名为projet,另一个名为statutprojet和关系ManyToOne。

我想计算项目并使用statutprojet组。

我试试这个,我用var_dump得到了这样的结果:

array (size=2)
0 => 
array (size=1)
  1 => string '1' (length=1)
1 => 
array (size=1)
  1 => string '2' (length=1)

我想用statutprojet libelle或id

显示结果

这是我的代码:

 $repo = $this   ->getDoctrine()
        ->getManager()
        ->getRepository('BackOfficeBundle:Projet');

    $qb = $repo->createQueryBuilder('p');
    $qb->select('COUNT(p)');
    $qb->groupBy('p.statutprojet');


    $projets = $qb->getQuery()->getArrayResult();

2 个答案:

答案 0 :(得分:0)

也许你的查询是这样的。

  $repo = $this ->getDoctrine()
                ->getManager()
                ->getRepository('BackOfficeBundle:Projet');

        $arrayCount = $repo->createQueryBuilder('p');
              ->join('p.statutprojet', 'sp')
              ->select('COUNT(p), sp.id')//or sp.name
              ->groupBy('p.statutprojet')
              ->getQuery()
              ->getArrayResult();

答案 1 :(得分:0)

You can try this :

    $repo = $this ->getDoctrine()
            ->getManager()
            ->getRepository('BackOfficeBundle:Projet');

    $qb= $repo->createQueryBuilder('p');

    $projects = $qb->join('p.statutprojet', 'sp')
          ->select('COUNT(p), sp.name')
          ->groupBy('sp.id')
          ->getQuery()
          ->getArrayResult();

And var_dump the result if it is still wrong i would like to see the result of this query and your 2 tables i would be able to help more.

相关问题