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) »
  • view custom fields in php
Pages: [1]

Author Topic: view custom fields in php  (Read 1308 times)

CatorgHans

  • I post frequently
  • ***
  • Posts: 157
  • Karma: 5
  • CiviCRM version: 4.4
  • CMS version: drupal
view custom fields in php
December 24, 2010, 03:14:53 am
When I save custom fields with civicrm_relationship_create they are stored, but I do not understand what suffix they get when I view it again.

Even when a new relationship is created, they can be returned with ['custom_5_9'] or ['custom_5_3'].
It is in a field where only one value is allowed.
When I save an existing relationship with the same values the suffix often is changed.

Is it true that the suffix can not be predicted when using the api?

And what would be the proper way in php to get the right value from a custom field when using civicrm_contact_relationship_get.
Is there a better then way then getting all the keys and looking for a key that starts with custom_x_ ?


Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: view custom fields in php
December 24, 2010, 05:34:02 am
I am not sure I understand your question entirely, but I will give you an answer which hopefully matches  ???

When you add a custom field set, a table is added to the database with the prefix civicrm_value and a number. So if I add a custom field set 'Blabla' and it is the first custom field set, a table named civicrm_value_blabla_1 will be created.
Also, a record will be added to the table civicrm_custom_group, with the name of the physical table as one of the fields.

For each custom field a record is added to civicrm_custom_field, with the incremental id of the table as a suffix. If I add a custom field 'nonsense' to my custom field set 'Blabla', and it is the third overall custom field in the configuarion, a record with the name 'custom_3' is added to civicrm_custom_field. Also, a field named 'nonsense_3' is added to the table civicrm_value_blabla_1.

Does this help?
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

CatorgHans

  • I post frequently
  • ***
  • Posts: 157
  • Karma: 5
  • CiviCRM version: 4.4
  • CMS version: drupal
Re: view custom fields in php
December 24, 2010, 05:41:41 am
thanks for the reply, but it is not what I meant.

If you get a relationship record with civicrm_contact_relationship_get and you look in the resulting array,
You will find the custom fields with their values in that array.

But not like ['custom_3'] = 'value', it will be ['custom_3_1'] = 'value'
OR and that is where my problem starts
 ['custom_3_2'] = 'value'
 ['custom_3_9'] = 'value'

That last number, after 'custom_3_' is what I call "suffix" in my original question and for me it is unclear why number that is or could be.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: view custom fields in php
December 24, 2010, 05:45:51 am
I see.....no idea....... :(
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

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: view custom fields in php
December 24, 2010, 07:01:56 am

1. the suffix cannot be predicted, since it is the id in the custom value table where that custom field is stored

2. multiple record custom group and single record custom groups share most of the same code, hence in single record the id of an existing value does not change

3. using that convention allows us to determine what row to update, which is what we use internally and hence is also exposed to the API

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

CatorgHans

  • I post frequently
  • ***
  • Posts: 157
  • Karma: 5
  • CiviCRM version: 4.4
  • CMS version: drupal
Re: view custom fields in php
December 27, 2010, 05:24:07 am
I think it would make sense to make an array of such a custom field values.

Now I will walk through results array with strpos function to get the right field.

Thanks for explaining.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: view custom fields in php
January 09, 2011, 02:17:32 am
I recently learnt there is a function to get the custom field number & row ID out of custom_23_1 without writing something yourself

    require_once 'CRM/Core/BAO/CustomField.php';
    list( $customFieldID, $customValueID ) = CRM_Core_BAO_CustomField::getKeyID($key, true );
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • view custom fields in php

This forum was archived on 2017-11-26.