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) »
  • Custom Hook Prior to DB Write for Cases
Pages: [1]

Author Topic: Custom Hook Prior to DB Write for Cases  (Read 271 times)

Richard Bramley

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.4.0
  • CMS version: Drupal 7
  • MySQL version: 5.6.13
  • PHP version: 5.4.16
Custom Hook Prior to DB Write for Cases
September 23, 2014, 06:40:39 am
I am trying to standardize the format of custom fields attached to a Case on save - that is, make them all upper case.  Initially I thought that the custom hook was the place for this to happen, but apparently it is only called after the values have been written to the DB.

Is there another hook that I could use?  Or does anyone have another suggestion as to how this could be achieved?

Thanks
« Last Edit: September 23, 2014, 06:47:23 am by Richard Bramley »

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Custom Hook Prior to DB Write for Cases
September 23, 2014, 07:15:21 am
a) Have you tried to simply do a SQL UPDATE in the current hook (ie after the record has been written)? That won't work for all use-cases, but it sometimes works. (The limitations: [1] doing novel SQL updates within hooks sometimes produces deadlocks; if you need it, there's a work-around that's helped me before.  [2] the SQL UPDATE generally wouldn't update PHP's in-memory data. Both limitations are subjective -- their relevance depends on the particular use-case, and the only way to be confident is to test.)

b) You might define a SQL trigger:

http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_trigger_info
https://github.com/civicrm/civicrm-core/blob/4.4/CRM/Utils/Hook.php#L1089

e.g.

Code: [Select]
  'table' => 'civicrm_value_my_table_123',
  'when' => 'BEFORE',
  'event' => array('INSERT', 'UPDATE'),
  'sql' => array(
    'NEW.my_field_456 = upper(NEW.my_field_456)'
  )

This tends to be more robust with multiple apps/use-cases/flows, but some folks report that administering triggers can be tricky on sophisticated mysql deployments (e.g. replicated mysql).

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Custom Hook Prior to DB Write for Cases

This forum was archived on 2017-11-26.