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) »
  • new sequential param (on contact_get)
Pages: [1]

Author Topic: new sequential param (on contact_get)  (Read 1044 times)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
new sequential param (on contact_get)
January 25, 2011, 07:23:26 am
http://api.crm/civicrm/ajax/rest?fnName=civicrm/contact/get&json=1&debug=1&sequential=1&return=contact_type,display_name

Code: [Select]
{
"is_error":0,
"count":3,
"values":[{
"contact_id":"1",
"contact_type":"Individual",
"display_name":"Xavier Dutoit"
},
{
"contact_id":"2",
"contact_type":"Organization",
"display_name":"Tech To The People"
},
{
"contact_id":"3",
"contact_type":"Organization",
"display_name":"toto"
},



The default (for contact) is still:

http://api.crm/civicrm/ajax/rest?fnName=civicrm/contact/get&json=1&debug=1&return=contact_type,display_name


Code: [Select]
{
"is_error":0,
"count":3,
"values":{
"1":{
"contact_id":"1",
"contact_type":"Individual",
"display_name":"Xavier Dutoit"
},
"2":{
"contact_id":"2",
"contact_type":"Organization",
"display_name":"Tech To The People"
},
"3":{
"contact_id":"3",
"contact_type":"Organization",
"display_name":"toto"
},



as of rev 32013.

I didn't introduce a new function because I found a way of hacking a default one:

Code: [Select]
    if (array_key_exists ('sequential',$params)) {
      return civicrm_create_success(array_merge($contacts));
    }

array_merge does reindex the keys if they are all numerical (the case for $contacts, as the contact id is the key)

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: new sequential param (on contact_get)
January 25, 2011, 07:29:02 am
FYI: we are still different from V2 (the results aren't under a "values" key but directly into the returned array) :

http://api.crm/civicrm/ajax/rest?fnName=civicrm/contact/get&json=1&debug=1&return_contact_type=1&return_display_name=1&version=2

Code: [Select]
{
"0":{
"contact_id":"1",
"contact_type":"Individual",
"display_name":"Xavier Dutoit"
},
"1":{
"contact_id":"2",
"contact_type":"Organization",
"display_name":"Tech To The People"
},
"2":{
"contact_id":"3",
"contact_type":"Organization",
"display_name":"toto"
},
"deprecated":"Please upgrade to API v3"
}


And just wrote something wrong: the key is an index, not the id of the contact (and now I remember why I really didn't like this format: that's both uglier than the normal array, not useful, and a pain to handle in js)

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: new sequential param (on contact_get)
January 25, 2011, 12:19:24 pm
That v2 example wasn't what you got in PHP - it must be REST specific - here's the phpUnit Test for contact_get in v2

Quote
    public function testContactGetRetFirst()
    {
        //  Insert a row in civicrm_contact creating contact 17
        $op = new PHPUnit_Extensions_Database_Operation_Insert( );
        $op->execute( $this->_dbconn,
                      new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
                             dirname(__FILE__)
                             . '/dataset/contact_17.xml') );
        $params = array( 'contact_id'       => 17,
                         'return_first_name' => true,
                         'sort'              => 'first_name' );
        $result = civicrm_contact_get( $params );
        $this->assertEquals( 2, count( $result[17] ) );
        $this->assertEquals( 17, $result[17]['contact_id'] );
        $this->assertEquals( 'Test', $result[17]['first_name'] );
    }
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: new sequential param (on contact_get)
January 26, 2011, 02:55:27 am
Ah, weird that it's different between rest and api. Will run a bit more testing on a 3.3 install with a little bit less dummy data.

Anyway, v2 is:

Code: [Select]
$result = civicrm_contact_get( $params );
        $this->assertEquals( 2, count( $result[17] ) );
        $this->assertEquals( 17, $result[17]['contact_id'] );
        $this->assertEquals( 'Test', $result[17]['first_name'] );


But v3 is either (sequential)

Code: [Select]
$result = civicrm_contact_get( $params );
$result = $result[results];
        $this->assertEquals( 2, count( $result[0] ) );
        $this->assertEquals( 17, $result[0]['contact_id'] );
        $this->assertEquals( 'Test', $result[0]['first_name'] );


or (non sequential)

Code: [Select]
$result = civicrm_contact_get( $params );
$result = $result[results];
        $this->assertEquals( 2, count( $result[17] ) );
        $this->assertEquals( 17, $result[17]['contact_id'] );
        $this->assertEquals( 'Test', $result[17]['first_name'] );


While we are at it, the syntax for the param is probably a few lines of change of being able to handle:


Code: [Select]
  $params = array( 'contact_id'       => 17,
                         'return' => 'first_name,last_name',
                         'sort'              => 'first_name' );
and
Code: [Select]
  $params = array( 'contact_id'       => 17,
                         'return' => array ('first_name,last_name'),
                         'sort'              => 'first_name' );

on the top of uglier:
Code: [Select]
  $params = array( 'contact_id'       => 17,
                         'return_first_name' => true,
                         'return_last_name' => true,
                         'sort'              => 'first_name' );


(already changed the code for the REST, just put it at the right place, might already work actually).
-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: new sequential param (on contact_get)
January 26, 2011, 03:07:47 am
I'm sure you just like changing things! I agree that it's nicer but as long as the original works I'm not going to change any tests around this one.

My next question though - bearing in mind the known occasional memory leak is     $ufJoinDAO->free; best practice & if so where - in this case an empty won't be returned.

In my import scripts the server would crash if I didn't add $dao->free -which I did liberally rather than systematically

Code: [Select]
    if ( ! $ufJoinDAO->find() ) {
        return civicrm_create_success(array());
    }

    while ($ufJoinDAO->fetch()) {
      _civicrm_object_to_array($ufJoinDAO, $ufJoin[$ufJoinDAO->id]);
    }
    $ufJoinDAO->free;
 
    if (array_key_exists ('sequential',$params)) {
      return civicrm_create_success(array_merge($ufJoin));
    }else{
      return civicrm_create_success($ufJoin);
    }
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: new sequential param (on contact_get)
January 26, 2011, 03:32:21 am

Quote from: Eileen on January 26, 2011, 03:07:47 am
I'm sure you just like changing things! I agree that it's nicer but as long as the original works I'm not going to change any tests around this one.

My point was that if the array result as on v3 is under a "values" and not directly under the result, we will have to change the test anyway,  no ?


Quote from: Eileen on January 26, 2011, 03:07:47 am

My next question though - bearing in mind the known occasional memory leak is     $ufJoinDAO->free; best practice & if so where - in this case an empty won't be returned.



No idea.

Lobo, you have wrangled the DB object beast to have an opinion ?
-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: new sequential param (on contact_get)
January 26, 2011, 03:33:31 am
Quote
I'm sure you just like changing things! I agree that it's nicer but as long as the original works I'm not going to change any tests around this one.
- I was meaning the return array
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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: new sequential param (on contact_get)
January 29, 2011, 11:05:32 am
Hi,

I just implemented the sequential for a couple of functions by adding &$params as an optional parameter for civicrm_create_contact and adding your clever bit of code in there. I think there are some potential down the track advantages of this:

1) I suspect that some functions may currently be altering the $params array & stripping non-BAO keys in which case the $params option would fail & we would pick this up

2) I think it's likely we might intro other formatting options later so passing params to the civicrm_create_success now will make it easier to do that later.



ALSO

I think that I STILL need to confirm what is agreed on the return params. My understanding is that create, get AND delete will all return in id indexed array of results (or sequential on the param)

At this stage delete & create only return one result but at some future point they might return more than one so returning

array('is_error' = 0,
         'version' = 1,
        'values'   =array(15=> array('id'=15)
       )

for a delete seems unweildly but has the advantage of being consistent with GET and is more future proof.
Some of the CREATE options return an array of values rather than just ID which I think is good. default for create & delete could be that 'is_sequential' is set?

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) »
  • new sequential param (on contact_get)

This forum was archived on 2017-11-26.