3

CiviCRM 4.6.8

I'm using Relationship.create in a civicrm post hook to create an inherited membership. My membership type is set to use this relationship. However after that API call is successful, the relationship exists but no membership follows.

I was reading the source for api/v3/Relationship.php and noticed this note for the setvalue method. It seems like I need to use it to invoke disableEnableRelationship.

However, I tried setting is_active to 1 with Relationship.setvalue in my hook function right after creating the Relationship and still no membership. Is this expected behaviour?

Here's roughly what I have:

$id = $relationship['id'];
$params = array(
       'id' => $id,
       'field' => "is_active",
       'value' => 1,
);
$result = civicrm_api3('Relationship', 'setvalue', $params);

Curiously, I notice if I edit the specific relationship in the admin UI and save, the proper inherited membership gets created.

2

I wouldn't use the setvalue method, it was introduced as a stop gap measure when the api v3 was young and it's deprecated now.

Is the relationship already active when you call the create call? setvalue isn't going to do anything more anyway.

I don't know the specific of that api, but in general, the api calls tend to be "close to the metal" and if there is a chain of events (eg if relationship is created and lots of configuration magic, then add a membership too), they are often not included by default.

On your specific case, if you are lucky it's just a matter of finding the right parameter to trigger the "advanced mode", or create the membership manually.

You should check out how it's done in the code for the UI, the code handling the membership creation might be already into the BAO layer and you can easily modify (or add an extra param) to the api to benefit from it.

If not, it might need some re-factoring....

  • Thanks for the ideas Xavier, that chain of events is (I think) hinted at in the code for setvalue and which I was trying to trigger by using it to set is_active. I explicitly set 'is_active' to 0 when I create the relationship but it doesn't appear to make a difference in creating the inherited membership. I will look into the code for the UI as per your suggestion, cheers! – schatz Sep 24 '15 at 20:08
  • I've tried just about every approach I can think of and even tried creating the membership myself. I dumped the membership each time the post_hook got passed a membership object and I see at least two memberships claiming they were created. Looking in log_civicrm_membership table, it looks like they get inserted but then immediately deleted. What would cause such a behaviour? – schatz Sep 25 '15 at 6:07
  • did you try switching from relationship "setvalue" to "create"? That should work better. – Coleman Sep 26 '15 at 17:15
1

We've been in a same situation after mass users import and ended up re-saving all Contacts via API call, using "Create" Contact and passing an existing IDs, as it has been recently introduced as "Update" call replacement. So no real changes were made, method works exactly as if you hit "Save" button in browser for each contact, but obviously much faster.

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.