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 (Moderator: Donald Lobo) »
  • Memberships Refreshing
Pages: [1]

Author Topic: Memberships Refreshing  (Read 1324 times)

chrisivens

  • Guest
Memberships Refreshing
June 03, 2009, 03:45:34 am
I need to alter a membership type so that a different relationship will cause contacts to inherit the membership. I've changed the id of the membership type needed in civicrm_membership_type table. I now need to refresh the relationships so that any old memberships are deleted and new ones created where appropriate.

I have written a chunk of code to try and do this but so far it isn't working. Code that I'm running is below if anyone can give me pointers. I based it on the postProcess of the relationships tab for a contact record.

Code: [Select]
require_once 'CRM/Contact/BAO/Relationship.php';
require_once('CRM/Core/Action.php');
//query to get relationships
$q = "SELECT * FROM {civicrm_relationship}";
$res = db_query($q);

//for each rel

while($row = db_fetch_array($res)) {

// Create vars needed for relationship membership stuff.
$contactId = $row['contact_id_a'];

$params = array (
    'hidden_custom' => 1,
    'relationship_type_id' => $row['relationship_type_id'] . '_a_b',
    'start_date' => array (
            'M' => null,
            'd' => null,
            'Y' => null,
        ),

    'end_date' => array (
            'M' => null,
            'd' => null,
            'Y' => null,
        ),

    'is_active' => 1,
    'description' => null,
    'note' => null,
);

$ids = array(
'contact' => $row['contact_id_a'],
'relationship' => $row['id'],
'contactTarget' => $row['contact_id_b'],
);

$action = CRM_Core_Action::UPDATE;

CRM_Contact_BAO_Relationship::relatedMemberships( $contactId, $params, $ids, $action, true );

// run it
}

lcdweb

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1620
  • Karma: 116
    • www.lcdservices.biz
  • CiviCRM version: many versions...
  • CMS version: Joomla/Drupal
  • MySQL version: 5.1+
  • PHP version: 5.2+
Re: Memberships Refreshing
June 03, 2009, 04:00:57 am
Just run the UpdateMembershipRecord.php script.
The only thing you may need to check on is if the old inherited memberships (based on the original relationship) were removed. I don't recall if those are removed, but am pretty certain that the new memberships via inheritance will update/be created based on the new relationship.
support CiviCRM through 'make it happen' initiatives!
http://civicrm.org/mih

chrisivens

  • Guest
Re: Memberships Refreshing
June 03, 2009, 04:04:44 am
Good call, I'll give it a go.

chrisivens

  • Guest
Re: Memberships Refreshing
June 03, 2009, 04:21:01 am
That seemed to work. Now for the kicker. I need to allow a one-to-many relationship. Rather that having one rel mapped to one membership I need several relationships to one membership. I had it sort of working but I think that the update script wiped out the memberships.

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Memberships Refreshing
June 03, 2009, 02:04:20 pm
Chris - unfortunately the current data model doesn't support this (civicrm_membership_type.relationship_type_id column represents a single relationship inheritance). Not sure if there's any easy / less drastic solution for solving this without wholesale changes to the schema and methods :-(
Protect your investment in CiviCRM by  becoming a Member!

chrisivens

  • Guest
Re: Memberships Refreshing
June 04, 2009, 12:29:46 am
Yeah, it's pretty obvious that to implement it properly would require a lot of reworking. Right now all I need is a workable hack really. I'm 90%+ through the project and I just recently got told the relationship was one-to-many.

I've managed to get the relatedMemberships code to accept my extra relationship type ids. Now all I need to do is make sure they are not removed by the update cron and also to go through and refresh the current relationships so any needed memberships are created.

chrisivens

  • Guest
Re: Memberships Refreshing
June 06, 2009, 03:11:25 am
I think I managed to hack something together. Must add the problem to my list ready for the dev camp.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Memberships Refreshing

This forum was archived on 2017-11-26.