Active
Project:
Group
Version:
8.x-1.x-dev
Component:
Group (group)
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
11 Feb 2019 at 10:25 UTC
Updated:
7 Aug 2020 at 14:53 UTC
Jump to comment: Most recent
Is there a performance way to count group users and members without using an static query?
Currently I'm using this and works:
$connection = \Drupal::database();
$query = $connection->query('SELECT count(id) as count FROM {group_content_field_data} WHERE gid = :id AND type LIKE :type', [
':id' => $group->id(),
':type' => $group->getGroupType()->id() . '-group_node-%',
]);
$count_contents = $query->fetchAll();
$query = $connection->query('SELECT count(id) as count FROM {group_content_field_data} WHERE gid = :id AND type LIKE :type', [
':id' => $group->id(),
':type' => $group->getGroupType()->id() . '-group_membership-%',
]);
$count_users = $query->fetchAll();
Static quering not seems a secure long-term approach, but If I use other ways like count getContents I totally lost performance:
$count_contents = count($group->getContent());
$count_users = count($group->getMembers());
It's there other performance way using entity query?
Comments
Comment #2
robertragas commentedCurrently experiencing the same issue, in my case I have a group with over 10k members and this times out every time.
Comment #3
jpoesen commentedNot sure if it's more performant or not, but I used this in the past:
Comment #4
ydahiHi guys, this is really helpful, thanks! Silly question: If I wanted to include a count of members or nodes in a view block, where would I add this code to?
Comment #5
zcht commentedthe YASM module also displays very detailed statistics for groups and users. could you have a look how it is done there, just as a hint :)
Comment #6
ydahiThank you! Exactly what I was looking for