1

I'm trying to use rules to send an email to all users with a specific role in an organic group when someone requests to join the group.

This code almost works. It returns all users from a specific group ($GID) with the specified role ($RID), but it doesn't restrict the role to that group. In other words, if they have the appropriate role in another group, they're included in the results.

$current_members = db_select('og_membership', 'om');
$current_members->join('og_users_roles', 'ogur', 'om.etid = ogur.uid');
$current_members->condition('om.gid', $GID);
$current_members->condition('om.entity_type', 'user');
$current_members->condition('ogur.rid', $RID); 

$result = $current_members->execute();
while ($res = $result->fetchAssoc()) { $members[] = $res['etid']; }

// Remove duplicate items.
$members = array_keys(array_flip($members));

What do I need to change to restrict the results so that it only returns users with the specified role ID within the specified group?

  • Why are you doing SQL on drupal??? That's what Views are for. With views, you can do all that easily. – Turion Mar 30 '15 at 9:40
  • There is absolutely nothing wrong with using db_select in drupal. I is a part of the Drupal api. I wouldn't advocate on using the php PDO driver directly, but in this case sTony is doing nothing wrong by using php --so long as it isn't in a php field :) – Frank Robert Anderson Apr 5 '17 at 16:59
1

It's just a simple sql query, no joins needed:

ie. select uid from og_users_roles where rid=3 and gid=835;

  $members = array();
  $current_members = db_select('og_users_roles', 'ogur');
  $current_members->fields('ogur', array('uid'));
  $current_members->condition('ogur.rid', $rid);
  $current_members->condition('ogur.gid', $gid);

  $result = $current_members->execute();
  while ($res = $result->fetchAssoc()) { $members[] = $res['uid'];}

  // Remove duplicate items.
  $members = array_keys(array_flip($members));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.