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) »
  • How to update contact display_name field directly
Pages: [1]

Author Topic: How to update contact display_name field directly  (Read 3244 times)

hlevinson

  • I post occasionally
  • **
  • Posts: 53
  • Karma: 3
How to update contact display_name field directly
February 02, 2010, 12:17:19 pm
I have a need to update the contact table's display_name field directly. I.e., I don't want CiviCRM to form it from the first and last names in the contact table.

I tried using civicrm_contact_add and civicrm_contact_update with display_name as a parameter, but it didn't update the db.

I assume this behavior is all by design?

If so, can someone suggest a good way to update this field? (I know I can use SQL UPDATE, wondering if there's some other way I've missed.)

If I update this field myself, will it get overwritten anytime someone edits the first or last name?

thanks

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: How to update contact display_name field directly
February 02, 2010, 12:37:34 pm

both display_name and sort_name are "computed" from first/last name and will be recomputed when saved again. Your best bet might be to either:

a. add a trigger in the DB that updates display_name when a contact is updated/inserted

b. add a hook_civicrm_post on the 'civicrm_contact' object to store your display name

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

hlevinson

  • I post occasionally
  • **
  • Posts: 53
  • Karma: 3
Re: How to update contact display_name field directly
February 02, 2010, 04:02:28 pm
I'm going to use both hook_civicrm_pre and hook_civicrm_post, to save and restore data from a custom field (in case anyone else has the same issue I did).

hlevinson

  • I post occasionally
  • **
  • Posts: 53
  • Karma: 3
Re: How to update contact display_name field directly
February 03, 2010, 12:35:11 pm
OK, now I'm pulling my hair out...

I implemented hook_civicrm_post, for the 'create' case for an Individual. Also tried the 'edit' case.

In there I want to write to the contact display_name field. drupal_write_record fails entirely, and when I try to use db_query to UPDATE I get this error (I do supply a value but the error message omits it):

Code: [Select]
user warning: Lock wait timeout exceeded;
try restarting transaction query:
UPDATE civicrm_contact SET display_name_en_US = ''
WHERE id = 166 in /path/mymodule.module on line 1202.

I looked at the triggers in MySQL, and they both run before an INSERT or UPDATE.

So how can I update this field? Or should I just give up?

My goal is to not modify the templates. I'm trying to fill in display_name programmatically, since that field is referred to all over the place.

geert.je

  • I’m new here
  • *
  • Posts: 16
  • Karma: 2
    • Flemish environment movement
  • CiviCRM version: 4.1.0
  • CMS version: Drupal 7
Re: How to update contact display_name field directly
February 23, 2010, 12:15:50 pm
I'm not sure but this I can use to overwrite a custom field after editing. I found it on the hooks page of this wiki:
http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmcustom
I suppose it can be changed to alter a field in the contact table too.

Maybe this can help.
I've just started to experiment with using hooks so don't shoot me if I'm wrong...
Geert

hlevinson

  • I post occasionally
  • **
  • Posts: 53
  • Karma: 3
Re: How to update contact display_name field directly
February 23, 2010, 12:21:10 pm
Thanks Geert, but if I understand your suggestion I don't think it will work because of the MySQL triggers -- they take precedence over everything.

The only way I found so far is to remove the triggers or edit them to stop the display_name update.

Harry



jyee

  • I’m new here
  • *
  • Posts: 19
  • Karma: 3
Re: How to update contact display_name field directly
September 29, 2010, 04:01:30 pm
any other solutions for the lock wait timeout problem?  Is this related to using the drupal db_query vs. some sort of civicrm db interface?

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to update contact display_name field directly

This forum was archived on 2017-11-26.