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) »
  • When is the best time to add relationships when saving a contact ?
Pages: [1]

Author Topic: When is the best time to add relationships when saving a contact ?  (Read 2059 times)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
When is the best time to add relationships when saving a contact ?
November 24, 2009, 08:20:05 am
Hi,

When saving a contact, I need to :
1) calculate a custom field and save it
2) add a relationship based on the value of this custom field

If I'm correct,
1) in civicrm_pre hook, I can create calculate the custom field and modify it, but I can't always create the relationship (because on create, the contact id isn't yet assigned

2) in civicrm_post hook, I can always create the relationship, but I don't understand how to save the custom field, because if I use the api, it will trigger again the civicrm_post hook, and I'll be looping between the api and the hook ?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

dragontree

  • Guest
Re: When is the best time to add relationships when saving a contact ?
November 29, 2009, 02:26:24 am
One way to get the contact id in the pre hook is to take it directly from the database. As the contact id field is autoincrement, the contact to be created will have the MAX(contact id)+1.

Not sure how this would behave if you have many users creating contact at the same time though. Probably not so well.

Not sure about the post hook...maybe add an if clause to check if the relationship is already created and if not, then create. Although this sounds too easy :D

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: When is the best time to add relationships when saving a contact ?
November 30, 2009, 05:36:23 am
Hi,

The problem is that the id is unique for all users. Meaning that between you do the max() and the time you insert the relationship, you might have another user that creates a contact, and therefore "stealing" the max() you have used in the relationship to create a different contact.

I don't think you can lock, especially if it mean locking between the pre and the post hook.

I'll try to cook the right test on the post.

X+


-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

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: When is the best time to add relationships when saving a contact ?
November 30, 2009, 08:15:20 am

1. you can set a static variable to prevent the looping between multiple calls of the hook. We use this trick in some of the drupal integration modules

2. You should not use a max(id) to compute your contact id. Ideally u should be able to get this from the "form" object in most cases. Unfortunately, we have not been consistent with class variables and this is specific on a form-by-form basis.

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: When is the best time to add relationships when saving a contact ?
November 30, 2009, 12:37:20 pm
THANKS!

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • When is the best time to add relationships when saving a contact ?

This forum was archived on 2017-11-26.