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) »
  • civicrm_create_success
Pages: [1]

Author Topic: civicrm_create_success  (Read 619 times)

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
civicrm_create_success
January 12, 2011, 06:16:15 am
In most API's civicrm_create_error is used to return error results, and in some API's the function civicrm_create_success is called to return valid results. In most cases the valid result is returned as an array. From v3 onwards the standard on paper will be:
  • use civicrm_create_error to return errors from the API. At some point we would like to introduce some standards in error messages, but I think that is still a little further on the path to perfection :-).
  • when valid results are returned from an API function, civicrm_create_success is used

For those amongst you that did not know this: both the civicrm_create_error and the civicrm_create_success function are in the CRM/api/v2/utils.php (at the moment, that will become CRM/api/v3/utils.php for the new version).

I have just committed the changed civicrm_create_success function for the v3 API. This is not in the core CiviCRM yet nor in the standard svn, but I do want to share what I have done and hopefully get some comments and feedback :-)

Here is the new code :
Code: [Select]
function civicrm_create_success( $params = 1 )
{
    $result = array();
    $result['is_error'] = 0;

    if (is_array($params)) {
        $result['count'] = count($params);

    } else {
        if (!empty($params)) {
            $result['count'] = 1;
        } else {
            $result['count'] = 0;
        }
    }
    $result['values'] = $params;
    return $result;
}

This means there will be one array with output, named 'result' that will always hold the following elements:
  • element is_error that will always have the value 0 is there is no error (which is obviously the case in civicrm_create_success) and the value 1 if there is an error (which will always be the case in civicrm_create_error)
  • the element count that will have a count of the elements in the array received in civicrm_create_success. So if there are more levels in the incoming array, it will count the elements at the top level
  • an element values that will hold the actual values of the result

Next steps for me now:
  • update the REST.php so it can cope with more than 2 levels in XML, which it currently can not
  • update civicrm_create_error
  • change all the existing API's to use civicrm_create_error and civicrm_create_success.
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: civicrm_create_success
January 12, 2011, 07:40:26 am
The big incompatible v3 warning label to be put in every release related documents:

Now the values returned aren't directly as key in the array returned (or first level in the json/xml on rest) but a key under the "values".

So if you where using an api

$result = civicrm_foo_bar ($param);
echo "the id is:". $result['id'];

you need to replace it by
$result = civicrm_foo_bar ($param);
echo "the id is:". $result['values']['id'];

For the the next A team meeting: I think we have a problem with civicrm_foo_get: sometimes it returns only one and only one item (eg. civicrm_tag_get), sometimes several (civicrm_contact_get).

Might want to introduce two actions (_get vs. _fetch) to identify ?

IMO, not very coherent to have["values"] when there is only maximum one item. ["value"] ?

Anyway, let's talk about it fri am

In the meantime: Eileen, happy birthday to you...

X+




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

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: civicrm_create_success
January 12, 2011, 08:20:23 am
Chat about it Friday, I am not entirely happy with the <values> either, but a standard way will be nice....by the way, the problem with having an extra level (so changing the $result['id'] to $result['values']['id'] would happen now too if we start using the civicrm_create_success consistently? I believe the problem is that we are not consistent in the API's anyway, so whatever we decide to do towards standardization will IMO always throw up problems with backward compatibility....the warning sign is required in any case for V3!

and Eileen, happy birthday from me too!!!!!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: civicrm_create_success
January 12, 2011, 10:52:22 am
Re GET vs Fetch - actually I think the tag API is wrong and it should allow more than one value to be retrieved - at the moment it only accepts 2 search options but I think it should accept more fields and do a search on 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

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

This forum was archived on 2017-11-26.