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) »
  • Queries during hook_civicrm_post
Pages: [1]

Author Topic: Queries during hook_civicrm_post  (Read 575 times)

samknelson

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
  • CiviCRM version: 4.6.4
  • CMS version: Drupal 7
  • MySQL version: 4.5
  • PHP version: 5.4
Queries during hook_civicrm_post
September 10, 2015, 06:08:53 am
I'm hoping for a little bit of guidance.

The objective: when a Individual is registered for an Event, I want to add an Activity attached to the *Organization* with which that Individual is associated.  (The idea being that you can go to an Organization's record and see registration Activities for all it's employees.)

My original plan had been to implement this logic during hook_civicrm_post.  When I detected a "create" operation on an object of type "Participant", I'd run some checks and then insert my own activity.

But I'm running into a number of issues, some of which I think are deadlocks.  In particular, if I try to run a query against the Contacts table during hook_civicrm_post, the web page never returns; I assume what's going on is that I'm trying to query against a table that's still in the middle of a transactional update.

So, a couple of questions:

- What's the lifecycle of hook_civicrm_post?  The documentation says that it's called "after a db write", but is there still a transaction active?  Is there any way to safely query the database during this hook?

- Or, is my entire approach wrong?  Should I be doing this some other way?

Thank you!

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Queries during hook_civicrm_post
September 10, 2015, 12:32:43 pm
Yeah, there should usually be an on-going transaction during hook_post. If the hook generates an error, it would be possible to rollback everything.

I've had one or two situations where I encountered a similar issue and worked around the deadlocks by deferring work until after the transaction. This generally looks like:

Code: [Select]
<?php
CRM_Core_Transaction
::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function(){
  
// Do the subsequent DB work.
});

You might want to take a little extra care to handle error-conditions within the callback.

Aside: There's some more material about transaction management at http://wiki.civicrm.org/confluence/display/CRMDOC/Transaction+Reference

samknelson

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
  • CiviCRM version: 4.6.4
  • CMS version: Drupal 7
  • MySQL version: 4.5
  • PHP version: 5.4
Re: Queries during hook_civicrm_post
September 11, 2015, 07:20:00 am
Thank you!  addCallback() is just what I was looking for.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Queries during hook_civicrm_post

This forum was archived on 2017-11-26.