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) »
  • Support »
  • Using CiviCRM »
  • Using Import (Moderator: Yashodha Chaku) »
  • condensing multiple memberships into one
Pages: [1]

Author Topic: condensing multiple memberships into one  (Read 1003 times)

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
condensing multiple memberships into one
November 24, 2010, 04:43:27 am
Hello,

This isn't really a 'using import' forum topic.  More a 'Import techniques' advanced import forum post.  Pete asked me about a script to condense multiple memberships into one.

Copied below is a script I used to do this once.  The memberships existed in one civicrm database and they got condensed in this script and transfered into another database.

The killer bit is

SELECT
    contact_id,
    membership_type_id,
    MIN(join_date) as join_date,
    MIN(start_date) as start_date,
    MAX(end_date) as end_date,
    source,
    status_id,
    is_override,
    reminder_date,

FROM
    `civicrm_membership`

WHERE is_test!=1

GROUP BY contact_id

This query will takes the min and max start and end date but the rest of the fields are taken at random from one particular row.  If you want to garuntee a particular row (say the latest row) you'll need to use a 'within-group aggregate' query - http://www.artfulsoftware.com/infotree/queries.php?&bw=1920#101

PS.  The includes at the top include some functions that allow you to translate from external id to civicrm id, put a database query a database into an array and not that much else :)

Hope this is helpful

Michael

Quote
<?php

include('../config.php');
include('/Users/michaelmcandrew/p/functions.php');

require_once "api/v2/Membership.php";
require_once 'api/v2/MembershipContact.php';


$eit=getEit();


$membersRaw=query('

SELECT
   contact_id,
   membership_type_id,
   MIN(join_date) as join_date,
   MIN(start_date) as start_date,
   MAX(end_date) as end_date,
   source,
   status_id,
   is_override,
   reminder_date,

FROM
   `civicrm_membership`

WHERE is_test!=1

GROUP BY contact_id

');

foreach($membersRaw as $memberRaw) {
   $member = $memberRaw;
   $member['contact_id'] = $eit[$memberRaw['contact_id']];
   $member['source'] = $memberRaw['fn'].' '.$memberRaw['ln'];
   $r = civicrm_contact_membership_create($member);
   if($r['is_error']){
      print_r($r);
      print_r($member);
   } else {
      echo '.';
   }
}


Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

cognoscento

  • I post occasionally
  • **
  • Posts: 44
  • Karma: 1
  • CiviCRM version: 4.2
  • CMS version: Drupal 7.17
  • MySQL version: 5.1.65
  • PHP version: 5.3.17
Re: condensing multiple memberships into one
January 17, 2011, 08:19:53 pm
This looks pretty much like what I was looking for.

I've got a whole bunch of duplicate (or overlapping) memberships that I want to clean up and merging them like this seems perfect. Would you be willing to provide a little more hand-holding to help me modify this to dedupe my memberships? Assuming I know very little php and SQL, but am willing to give it a shot, where should I start?

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: condensing multiple memberships into one
January 19, 2011, 06:02:56 am
good way to start would be describing your use case in more detail.  are all of these in one civicrm db? are you doing an import? can you give an example of the merge you want to do?
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: condensing multiple memberships into one
November 17, 2011, 10:52:41 am
Just wanted to add another couple of useful search terms here since I couldn't find it easily last time i looked: merge and consolidate :)
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Import (Moderator: Yashodha Chaku) »
  • condensing multiple memberships into one

This forum was archived on 2017-11-26.