I couldn't find "GROUP BY" method for queries made by EntityFieldQuery.
is there a way to do it ?

Comments

fuerst’s picture

Nope, there isn't. At least not when looking at DRUPAL7/includes/entity.inc.

bojanz’s picture

Status: Active » Fixed

Yep, no GROUP BY.

chx’s picture

Title: Does EntityFieldQuery support "GROUP BY" ? » Add some "GROUP BY" support to EntityFieldQuery
Version: 7.x-dev » 8.x-dev
Category: support » task
Status: Fixed » Active
donutdan4114’s picture

Version: 8.x-dev » 7.x-dev
Status: Active » Needs review
StatusFileSize
new3.71 KB

Testing entity groupBy patch for D7.

donutdan4114’s picture

StatusFileSize
new3.71 KB

Updated patch to most recent version of Drupal core.

Status: Needs review » Needs work

The last submitted patch, entity-groupby-1565708-5.patch, failed testing.

donutdan4114’s picture

StatusFileSize
new3.68 KB

Fixed path issue in patch.

chx’s picture

Status: Needs work » Needs review

You need David Rothstein to decide this but I am fairly sure this is a won't fix. We are working on this for the new entity query for D8 but in D7, I do not think this is a viable idea.

chx’s picture

Assigned: Unassigned » David_Rothstein

So, David, should we pursue this?

donutdan4114’s picture

Why would it not be a viable idea? Do new features not get added to D7?
I think it would be great to have. EntityFieldQuery supports other basic operations like orderBy.. Not sure why groupBy wasn't included... Is there a reason?

chx’s picture

New features in general go against D8, small API additions occasionally make D7. The decision lies with the D7 maintainer.

donutdan4114’s picture

It should be noted that the patch in #7 is not complete.
The fieldGroupBy logic is not complete. I wasn't sure where to put it.

I tried to reference fieldOrderBy, but couldn't find where the logic happened.

David_Rothstein’s picture

Assigned: David_Rothstein » Unassigned

Oops, apparently this has been assigned to me for the last six months. Sorry :(

I don't think I have much to add beyond what's already at https://drupal.org/node/1527558. Small API additions are allowed. And one thing this patch definitely has going for it is that it appears to be completely backwards compatible.

In short, I don't really have a strong feeling either way on this one; so if the community decides adding GROUP BY support is a good idea for Drupal 7 still, I'd be willing to commit a patch that does that.

gusantor’s picture

It would be great to have groupby on EFQ :)

chx’s picture

The big problem, sorry for not realizing this back in January is that your return value can only be the results of some aggregation function or the GROUP BY column. Now, our return values are entity ids. I do not know how to solve this without changing the output format. D8 EntityQuery has a very different return format for aggregator and normal queries to boot.

sylvain lecoy’s picture

I would love to see this for D7 as well.

sylvain lecoy’s picture

chx, if developers does not use the fieldGroupBy, the output does not change isn't it ?

So we maintain compatibility, and this change remains backward compatible. An API changes is not necessarily bad when adding a new method, as it does not create regressions.

sylvain lecoy’s picture

Status: Needs review » Needs work

I also tested this patch and see nothing changing when trying to group by.

The result set looks exactly the same. Not sure how to use this patch.

sylvain lecoy’s picture

Seems this is not implemented for fields actually !

donutdan4114’s picture

Correct, not implemented for fields. I was hoping someone could help me with that.

donutdan4114’s picture

Status: Needs work » Needs review

#7: entity-groupby-1565708-7.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, entity-groupby-1565708-7.patch, failed testing.

Dumitru Grosul’s picture

Issue summary: View changes

You ca use query tags to achieve this. Add a custom tag to the query and implement the hook_query_TAG_alter().

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
...
    ->addTag('MY_TAG');

  // Execute the query.
  $result = $query->execute();
/**
 * Implements hook_query_TAG_alter().
 */
function MY_MODULE_query_MY_TAG_alter(QueryAlterableInterface $query) {
  $query->groupBy('TABLE_ALIAS.FIELD');
}
reign85’s picture

I would love to see this for D7 as well

Chris Charlton’s picture

Stalled?

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.