2

I am trying to create a lists of contact along with the membership type associated with each contact. Below is the code I have:

$result = civicrm_api3('Contact', 'get', array(
    'sequential' => 1,
    'return' => "tag,display_name,id,image_URL",
    'contact_type' => "Organization",
    'options' => array('limit' => 300),
  ));

I am not sure how to do join api call to both contact and membership, so that I can get all detail in one single api call.

so I am wondering if anyone can help.

So, far I have created another api call to each contact list returned form the above api call, like this:

foreach($result['values'] as $member){
    $ufmatch = civicrm_api3('Membership', 'get', array(
      'sequential' => 1,
      'contact_id' => $member['id'],
    ));
    $member['member_type'] = $ufmatch['values'][0]['membership_name'];

    $nodes[] = array(
      'node' => $member,
    );
  }

$members = array(
    'nodes' => $nodes,
  );

  return drupal_json_output($members);

But there are hundreds of contact list. So, it takes almost 2-3 mins load all data.

Can anyone please guide me in a right direction. Thanks in advance.

1 Answer 1

1

You need to do it the other way around. Membership is the main entity and then you join on Contact. The api explorer can help put it together but you're looking for something like:

$result = civicrm_api3('Membership', 'get', [
  'sequential' => 1,
  'return' => ["contact_id.id", "contact_id.display_name", "membership_type_id"],
]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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