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) »
  • Hook for custom member ID field
Pages: [1]

Author Topic: Hook for custom member ID field  (Read 1536 times)

sjthespian

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 3
    • The League of Professional System Administrators
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.28
  • MySQL version: 5.1.66
  • PHP version: 5.3.3
Hook for custom member ID field
August 19, 2010, 10:11:42 am
We have the need to assign a custom member ID for anyone who gets a membership on our site.  With a few exceptions that will be handled manually, the member ID is the current maximum member ID +1.  I have a custom field created for this already (and populated with our current membership), but I wanted to get a sanity check on the hook I have written for updating this value for new members before I deploy it since I haven't written a CiviCRM hook before...

The algorithm I think I am using is (this should only be run on a new membership):
  • Check for an existing ID for the contact, if one exists do nothing
  • If one doesn't exist, get the highest ID from the custom data table and add 1
  • Assign the new ID to the contact

Code: [Select]
/*
 * Post-process membership forms to make sure we have a memberID
 */
function CiviLOPSA_civicrm_postProcess( $formName, &$form ) {
  /* Don't do anything of not a new membership */
  if ($formName == 'CRM_Member_Form') {
    $userID   = $form->getVar( '_id' );

    /* Query the memberID custom field for the max and add 1 for new ID */
    $query = "
SELECT member_id_10
FROM civicrm_value_member_data_4
WHERE entity_id = %1
";
    $params = array( 1  => array( $userID, 'Integer' ));
    $dao = CRM_Core_DAO::executeQuery( $query, $params );

    /* If one exists we're done, otherwise generate a new one */
    if (! $dao->fetch()) {
       $query = "
SELECT max(member_id_10)
FROM civicrm_value_member_data_4
";
      $dao = CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray );
      if ( $dao->fetch() ) {
        $memberID = $dao->member_id;
        $memberID++;
        $params = array('entity_id' => $userID,
                        $custom_fields['member_id'] => $memberID);
        CRM_Core_BAO_CustomValueTable::setValues($params);
      }
    }
  }
}
Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Hook for custom member ID field
August 19, 2010, 10:26:25 am

any specific reason why u r not using the 'id' column from the civicrm_membership table?

it basically has exactly the same rules (its an auto-increment field)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

sjthespian

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 3
    • The League of Professional System Administrators
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.28
  • MySQL version: 5.1.66
  • PHP version: 5.3.3
Re: Hook for custom member ID field
August 19, 2010, 10:48:39 am
Quote from: Donald Lobo on August 19, 2010, 10:26:25 am

any specific reason why u r not using the 'id' column from the civicrm_membership table?

it basically has exactly the same rules (its an auto-increment field)

lobo


The above only applies to new users, we have existing users with numbers all over the map.  We have a couple of member classes that allow the user to select a member ID, and we still need to support that.  An auto-increment field will give us problems down the road when we need to override the value.
Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

sjthespian

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 3
    • The League of Professional System Administrators
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.28
  • MySQL version: 5.1.66
  • PHP version: 5.3.3
Re: Hook for custom member ID field
October 11, 2010, 06:53:07 pm
Just to follow up, we abandoned this id.  As Donald suggested we decided to use the contact_id for the membership id.

If anyone else ever goes down this path, we did have to modify the database schema to add constraints to update the contact_id if it is changed in the civicrm_contact table.  There are constraints in the installed schema for delete but nothing for update.
Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Hook for custom member ID field

This forum was archived on 2017-11-26.