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) »
  • Basic query about using api to return only certain fields
Pages: [1]

Author Topic: Basic query about using api to return only certain fields  (Read 659 times)

alo

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.4.3
  • CMS version: wordpress 3.9
  • MySQL version: 5.0.77 client
  • PHP version: 5.3.28
Basic query about using api to return only certain fields
June 20, 2014, 05:14:53 am
Hi,

I am using the api code as follows;

$params = array( 'version'  => 3, 'options' => array('sort'=>' contact_id DESC', 'limit'=>10 ) );
$result = civicrm_api( 'Contact' , ' get' , $params );

this returns all the fields for contacts, but I just need the image_URL and display_name values. I know theres a 'return' param I can use, but I cannot find anywhere that states what format it is expecting the list of return fields. I tried adding them comma separated as below (and also as an array), but the entire dataset is still returned.

e.g
$params = array( 'version'  => 3, 'options' => array('sort'=>' contact_id DESC', 'limit'=>10,'return'=>'image_URL,contact_id' ) );
$result = civicrm_api( 'Contact' , ' get' , $params );

I'm sure this is a simple question - help much appreciated though!

Al.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Basic query about using api to return only certain fields
June 20, 2014, 06:21:04 am
Hi,

you put the return as part of the option, it should be a top level key.

BTW, we recommend using the newer civicrm_api3:


Code: [Select]
$params = array( 'options' => array('sort'=>' contact_id DESC', 'limit'=>10 ), return=> "image_URL" );
$result = civicrm_api3( 'Contact' , ' get' , $params );

The main difference is that if you have an error, it will throw and exception and crashes "cleanly" if you forget to handle the case properly instead of silently let you continue running the code.

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Basic query about using api to return only certain fields
June 20, 2014, 09:07:26 am
xavier - I recently got burned by this also (putting "return" in the options array rather than at the top level). This seems silly, is there any reason not to support it as part of options, that seems to make more sense all-around.
Try asking your question on the new CiviCRM help site.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Basic query about using api to return only certain fields
June 20, 2014, 10:22:11 am
I'm afraid the only reason it's at the top level is because it was there before the option was set.

You suggest we honor both the return and option=>return and obsolete the top level return?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Basic query about using api to return only certain fields
June 20, 2014, 01:55:23 pm
That seems reasonable - there is a long tail of syntaxes supported for 'return' - but there are tests for all of them
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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Basic query about using api to return only certain fields
June 20, 2014, 02:07:38 pm
Quote
You suggest we honor both the return and option=>return and obsolete the top level return?

This late in the api3 lifecycle I'm not sure we should obsolete anything, but yes I think it would make sense to support it as an option as well as the top level.
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Basic query about using api to return only certain fields
June 20, 2014, 03:56:53 pm
lifecycle or deathspiral.....
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) »
  • Basic query about using api to return only certain fields

This forum was archived on 2017-11-26.