如何以编程方式列出有机组织的成员实体?


17

如何以编程方式获取有机组织的成员实体列表?

(我知道如何使用“视图”获取群组成员的列表-我更喜欢使用“有机群组” API来执行此操作,但一直未能找到正确的方法。)

Answers:


14

直接使用有机群组API:

og_get_group_members_properties($yourGroupNode, array(), 'members', 'node');

为您提供了一组用户ID。

要仅获取活动,待处理或已阻止的用户,可以将“ members__1”,“ members__2”或“ members__3”代替“ members”,例如以下示例:

og_get_group_members_properties($yourGroupNode, array(), 'members__1', 'node');

的,如果您愿意,像这样的一个:

og_get_group_members_properties($yourGroupNode, array(), 'members__' . OG_STATE_ACTIVE, 'node');

返回值的示例:

Array
(
    [0] => 48
    [1] => 49
    [2] => 51
)

1
这应该是正确的答案
Chris

11

或通过DBTNG:

$query = db_select("og_membership", "ogm");
$query->condition("ogm.gid", $yourGroupID, "=");
$query->condition("ogm.group_type", "node", "=");
$query->fields("ogm", array("entity_type", "etid"));
$result = $query->execute();
print_r($result->fetchAll());

...这使您可以联接其他表,而不必像使用一样过滤PHP代码中的结果集EntityFieldQuery



2

smokris解决方案有效,但我发现了另一种方法EntityFieldQuery

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
  ->fieldCondition('og_group_ref', 'target_id', $yourGroupID)
  ->execute();

返回值的示例:

{"node":
  {"22":{"nid":"22","vid":"22","type":"panoramique"},
   "32":{"nid":"32","vid":"32","type":"panoramique"},
   "35":{"nid":"35","vid":"35","type":"panoramique"},
   "36":{"nid":"36","vid":"36","type":"panoramique"}
  }
}

0

扩展@smokris的答案。

成员资格的活动状态为“ 1”(->condition('ogm.state', 1, '='))。

function _get_users_in_group($gid) {
  $query = db_select('users', 'u');

  $query
    ->condition('u.uid', 0, '<>')
    ->condition('u.status', 1, '=')
    ->condition('ogm.state', 1, '=')
    ->fields('u', array('uid', 'name'))
    ->join('og_membership', 'ogm', "ogm.gid = :gid AND u.uid = ogm.etid AND ogm.entity_type = 'user'", array(':gid' => $gid));
  return $query->execute();
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.