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) »
  • API with multiple record custom fields
Pages: [1]

Author Topic: API with multiple record custom fields  (Read 1135 times)

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
API with multiple record custom fields
October 03, 2012, 07:21:12 am
I am trying to use the API to retrieve data from custom data sets I am using in a profile. The custom data has multiple rows for a contact. I don't know how to retrieve the row ids to add to the field name (e.g. custom_50_5) so they will be there when the user saves when editing.

Here is what I did:
Code: [Select]
require_once ('api/api.php');

/*
* retrieve the fields
*/
$params=array(
'version' => 3,     
'sequential' => 1,
'custom_group_id' =>$groupID
);
$result = civicrm_api ('CustomField','GET',$params);

/*
* store in array
*/
foreach($result['values'] as $k => $field){
$field_attributes[$field['id']] = $field;
}

/*
* prepare param for retrieving values
         */
$keys = array_keys($field_attributes);
$params =array(
'version' => 3,     
'sequential' => 1,
'entity_id' => $cid,
'custom_group_id' =>$groupID,
'entity_table' =>'',
);
foreach ($keys as $n => $id){
$params["return.custom_$id"] = '1';
}
$result = civicrm_api ('CustomValue','GET',$params);


This worked ok, but the result was without a row id for the custom values :
e.g.:
        (
           
  • => Array

                (
                    [entity_id] => 2
                    [latest] => 75
                    [id] => 25
                    [1] => 75
                )

            [1] => Array
                (
                    [entity_id] => 2
                    [latest] => 50
                    [id] => 27
                    [1] => 50
                )

How could I do this in a way that I get the row id as well as the column id?
Thanks,
Jere



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: API with multiple record custom fields
October 05, 2012, 02:00:52 am
I would guess that does not work yet in the API. The option we would all really appreciate and applaud is if you fix the API? Ping me, Eileen or Xavier on IRC if you need any help....there is a guideline here: http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards

The quick and dirty way (but sometimes useful  ;D) is to retrieve the data direct from the tables you need using the CRM_Core_DAO::executeQuery( $yourqry )? So something like this:
Code: [Select]
$query = "SELECT id, name, date FROM civicrm_value_blah WHERE entity_id = $contact_id";
$daoBlah = CRM_Core_DAO::executeQuery( $query );
while ( $daoBlah->fetch() ) {
  $name = $daoBlah->name
  and do other stuff
}
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
Re: API with multiple record custom fields
October 05, 2012, 06:25:52 am
Thanks Erik.

I actually used the approach you suggested before trying the api.

With all the talk about the database one day changing and the benefits to using the api, I was trying to adapt to the api approach. Right now I have some urgency about completing this project, so I will have to wait a couple of weeks, but I would love to get more involved with civicrm. I will study the link you provided and propose code changes to add the row_id.


Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API with multiple record custom fields
October 07, 2012, 05:27:36 pm
Isn't the id (as above) the row id. There was a huge discussion about this api as the underlying problem is that the custom values don't fit  the normal table 1 table = 1 entity
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

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
Re: API with multiple record custom fields
October 08, 2012, 12:12:02 pm
The id in my case (25 in this clip from debug output)
(e.g. for this return )
                (
                    [entity_id] => 2
                    [latest] => 75
                    [id] => 25
                    [1] => 75
                )
 is the id as found in civicrm_custom_fields. The row_id I need is the row in the new table with the name listed in civicrm_custom_group.table_name. The row id is the id of that table, the x field in page element names formatted like custom_25_x.  It may only be needed for multiple-record custom fields, but I am not sure. I am only struggling with multiple record fields now.


Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API with multiple record custom fields
October 08, 2012, 01:19:06 pm
OK & is the row id in that example '1'?
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

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
Re: API with multiple record custom fields
October 08, 2012, 02:50:27 pm
Duh. I reverted to that code and ran it again, and '1' is the row id. Thanks for your patience. I guess I was looking for it on the right side of the => or something.

Jere



Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API with multiple record custom fields
October 08, 2012, 03:19:09 pm
It IS a confusing api - but after a LOT of agonising we couldn't come up with something better.
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

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
Re: API with multiple record custom fields
October 08, 2012, 08:13:46 pm
I love the api approach. I am new to it, though, and there is a lot to absorb in a short time.

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: API with multiple record custom fields
October 09, 2012, 12:15:15 am
I recognize that emotion  ;D Keep going!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

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

This forum was archived on 2017-11-26.