SGarofalo808
SGarofalo808

Reputation: 167

Doctrine "group by" doesn't returns all existing objects

I have a table with user alarms, I would like to get a list of ALL the alarms grouped by the user who created them.

$qb = $this->em->getRepository('AppBundle:Alarm')->createQueryBuilder('a')->groupBy('a.userId');
$result = $qb->getQuery()->getResult();

The problem is that I'm only getting one alarm per user instead of all the existing alarms for him.

If I remove the group by I get all the alarms.

Is there any way I can get all the alarms grouped by user? I was expecting to get and array of arrays.

Upvotes: 1

Views: 521

Answers (1)

Jeet
Jeet

Reputation: 1587

This is working just the way a groupBy clause should work. It picks the first Alarm item for each of user.

If you want all Alarm for a single User, you have to work along with group_concat which again doesn't come with Doctrine. You have to register a custom DQL function. Check here

Upvotes: 1

Related Questions