Reputation: 41
I build a symfony project but I have an issue with doctrine query builder.
I have two entities one named projet and the other named statutprojet and relation ManyToOne.
I want to count projects and use group by statutprojet.
I try this and i get the result like this with a var_dump:
array (size=2)
0 =>
array (size=1)
1 => string '1' (length=1)
1 =>
array (size=1)
1 => string '2' (length=1)
i want to display the result with statutprojet libelle or id
this is my code :
$repo = $this ->getDoctrine()
->getManager()
->getRepository('BackOfficeBundle:Projet');
$qb = $repo->createQueryBuilder('p');
$qb->select('COUNT(p)');
$qb->groupBy('p.statutprojet');
$projets = $qb->getQuery()->getArrayResult();
Upvotes: 0
Views: 218
Reputation: 2263
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.
Upvotes: 0
Reputation: 136
Maybe your query like this.
$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();
Upvotes: 0