CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Getting the most recent membership?
Pages: [1]

Author Topic: Getting the most recent membership?  (Read 859 times)

snowball

  • I’m new here
  • *
  • Posts: 16
  • Karma: 0
  • CiviCRM version: 3.4
  • CMS version: Drupal 6
  • MySQL version: 5
  • PHP version: 5
Getting the most recent membership?
November 14, 2011, 02:56:56 am
I'm just starting to dabble with the CiviCRM v3 API and am presently porting an older drupal plugin to use this API.  I'm trying to replace lines like:

db_query("SELECT id,start_date,end_date FROM {civicrm_membership} WHERE contact_id=:cid ORDER BY end_date DESC", array(':cid' => $cid));

and

db_update("UPDATE civicrm_membership SET end_date = SYSDATE WHERE contact_id = :cid ORDER BY end_date DESC", array(':cid' => $cid));

Is it possible to recreate this functionality using the API, namely, is it possible for an API call to only return or update the membership with the most recent end date?

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Getting the most recent membership?
November 14, 2011, 05:40:48 am
Without checking in detail I would assume you can retrieve the membership with the most recent end date using the API (entity=Membership, action=Get) and then using the id you have retrieved, update it? Did you check the API documentation here:
http://en.flossmanuals.net/civicrm-developer-guide/api/

and the specifics of the membership API with the API explorer mentioned in the documentation?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

snowball

  • I’m new here
  • *
  • Posts: 16
  • Karma: 0
  • CiviCRM version: 3.4
  • CMS version: Drupal 6
  • MySQL version: 5
  • PHP version: 5
Re: Getting the most recent membership?
November 14, 2011, 07:01:00 am
Have had a look at the API documentation, can certainly retrieve a membership based on some criteria however in terms of date it seems to only allow searching for memberships that have a specific end date, rather than the most recent end date.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Getting the most recent membership?
November 14, 2011, 11:12:47 pm
Hmmmm no glory  :(
I have 2 options for you:
  • raise an issue on http://issues.civicrm.org/jira/secure/CreateIssue!default.jspa and someone from the API team will look into it as soon as they can (accepting the fact that they are all community supporters with day time jobs)
  • if you have enough PHP skills, dive into the API structure, fix the problem and share it with us as a patch.....and that would be great! Someone from the API team will usually be available on the civicrm IRC channel to help you out
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Getting the most recent membership?
November 14, 2011, 11:23:26 pm
There is some work needed on the API.

The issue is that if contact_id is passed in it calls a BAO function which supports the param ['active_only'] but not much else.

If contact id is passed in it calls basic_get which supports the options 'sort', 'limit', 'offset' & 'return' => (array of fields to return) along with filter.start_date_high & filter.start_date_low

Probably it should be made to handle 'active_only' within the api & to always call basic_get (& of course a test should be added)

Code: [Select]
    CRM_Member_BAO_Membership::getValues( $membershipParams, $membershipValues, $activeOnly );
    if(empty($params['contact_id'])){
      //added this as contact_id was the only acceptable field so this was a quick way to improve
        $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
    }
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Getting the most recent membership?

This forum was archived on 2017-11-26.