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) »
  • Mr & Mrs GET
Pages: [1]

Author Topic: Mr & Mrs GET  (Read 940 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Mr & Mrs GET
February 11, 2011, 12:07:15 am
I need to get option groups & values today so I tried using boilerplate api code as a starting point - look @ the two working functions pasted below- there is only word difference - does that suggest anything to anyone?

Code: [Select]
function civicrm_helpers_get_optiongroup($params){
try {
  _civicrm_initialize( true );
       require_once 'api/v2/utils.php';
    civicrm_verify_mandatory($params);
    require_once 'CRM/Core/BAO/OptionGroup.php';
    $bao = new CRM_Core_BAO_OptionGroup();
    $fields = array_keys($bao->fields());

    foreach ( $fields as $name) {
        if (array_key_exists($name, $params)) {
            $bao->$name = $params[$name];
        }
    }
   
    if ( $bao->find() ) {
      $results = array();
      while ( $bao->fetch() ) {
        _civicrm_object_to_array( $bao, $result );
        $results[$bao->id] = $result;
      }
      return civicrm_create_success($results,$params,$bao);
    } else {
      return civicrm_create_success(array(),$params,$bao);
    }

  } catch (PEAR_Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  } catch (Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  }
  }
 

function civicrm_helpers_get_optionvalues($params){
try {
   _civicrm_initialize( true );
       require_once 'api/v2/utils.php';
   civicrm_verify_mandatory($params);
    require_once 'CRM/Core/BAO/OptionValue.php';
    $bao = new CRM_Core_BAO_OptionValue();
    $fields = array_keys($bao->fields());
    foreach ( $fields as $name) {
        if (array_key_exists($name, $params)) {
            $bao->$name = $params[$name];
        }
    }
   
    if ( $bao->find() ) {
      $results = array();
      while ( $bao->fetch() ) {
        _civicrm_object_to_array( $bao, $result );
        $results[$bao->id] = $result;
      }
 
      return civicrm_create_success($results,$params,$bao);
    } else {
      return civicrm_create_success(array(),$params,$bao);
    }

  } catch (PEAR_Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  } catch (Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  }
  }
 
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Mr & Mrs GET
February 11, 2011, 12:30:23 am
Shouldn't be in a helper, but promoted as first class entities

Beside that, yeap, some cleanup and explanations on what helpers... and still some chunk of boilerplate code still.

Beside that I'll svn blame to check with the authors later.

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: Mr & Mrs GET
February 11, 2011, 06:33:51 pm
erm - ignore the function name - that's because I was implementing a function on a site that doesn't support v3 api - that's my helper modules name.

 - I was more pointing out that in order to create 'missing' functions I was able to paste in  the code with only one word changed & it worked. It made me think maybe we should be able to do a GET on pretty much any entity for which there is no existing api using this basic function - although the problem with a 'give-it-a-go-get' function is that it would be missing tests & really the main point of using the api rather than just querying the DB directly is that the api should be tested & the output should not change when the DB does.
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Mr & Mrs GET
February 12, 2011, 04:29:13 am
Hi,

I'm giving a go today at the civicrm(whatever,"modify")

Check the code, I'm going to try to see if I can go a generic one.

The idea would be that if civicrm_entity_get exists, call it, if not, use the generic one (like for modify)

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

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

This forum was archived on 2017-11-26.