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

Author Topic: create_success ... again  (Read 637 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
create_success ... again
January 24, 2011, 02:09:52 am
Hi,

Haven't commited this yet but trying to figure out the civicrm_create_success function. It seems some functions (like contact_create) start get given an array indexed by id whereas others e.g tag_create get a sequentially indexed array as their starting point

For REST uses I think the sequential is almost always better whereas for std PHP the one indexed by id is probably better so ideally the civicrm_create_success would take either form in & be able to convert it to the other if required. I tried this patch which worked in the context of UFJoin.php (copied from Tag ) but it doesn't do the third option of converting an id-indexed one back to a sequentially indexed. (My tired brain is trying to turn 3 possibilies into a boolean)

Code: [Select]
Index: api/v3/UFJoin.php
===================================================================
--- api/v3/UFJoin.php (revision 31984)
+++ api/v3/UFJoin.php (working copy)
@@ -102,7 +102,11 @@
     while ($ufJoinDAO->fetch()) {
       _civicrm_object_to_array($ufJoinDAO, $ufJoin[]);
     }
-    return civicrm_create_success($ufJoin);
+    //set to reindex if $params['sequential'] not set - this is because result is indexed
+    //in sequential order in this function whereas in some functions it is id indexed
+    //so for example in the contact API which is indexed by ID (by default) this clause would be reversed
+    $reindex = ($params['sequential']? 0 : 1);
+    return civicrm_create_success($ufJoin,$reindex);
   
   } catch (PEAR_Exception $e) {
     return civicrm_create_error( $e->getMessage() );
Index: api/v3/utils.php
===================================================================
--- api/v3/utils.php (revision 31981)
+++ api/v3/utils.php (working copy)
@@ -79,7 +79,7 @@
  */
 function civicrm_verify_mandatory (&$params, $daoName = null, $keys = array() ) {
   if ( ! is_array( $params ) ) {
-     throw new Exception ('Input parameters is not an array');
+     throw new Exception ('Input variable `params` is not an array');
   }
 
   if ($daoName != null) {
@@ -122,10 +122,10 @@
 
 /**
  *
- * @param <type> $result
- * @return <type>
+ * @param array $result
+ * @return array
  */
-function civicrm_create_success( $params = 1 )
+function civicrm_create_success( $params = 1, $reindex = 0, $idfield = 'id' )
 {
     $result = array();
     $result['is_error'] = 0;
@@ -140,7 +140,13 @@
             $result['count'] = 0;
         }
     }
-    $result['values'] = $params;
+    if ($reindex){
+      foreach ($params as $key =>$values){
+        $result['values'][$values[$idfield]] = $values;
+      }
+    }else{
+      $result['values'] = $params;
+    }
     return $result;
 }
 /**
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: create_success ... again
January 24, 2011, 04:40:23 am
Quote from: Eileen on January 24, 2011, 02:09:52 am

Haven't commited this yet but trying to figure out the civicrm_create_success function. It seems some functions (like contact_create) start get given an array indexed by id whereas others e.g tag_create get a sequentially indexed array as their starting point

That's what I get when calling
http://api.crm/civicrm/ajax/rest?fnName=civicrm/contact/create&json=1&debug=1&contact_type=Organization&organization_name=toto&version=3

Code: [Select]
{
"contact_id":42,
"is_error":0
}

Not sure I understand what you mean about the array indexed by id. Could you clarify ?

Quote from: Eileen on January 24, 2011, 02:09:52 am


For REST uses I think the sequential is almost always better whereas for std PHP the one indexed by id is probably better so ideally the civicrm_create_success would take either form in & be able to convert it to the other if required. I tried this patch which worked in the context of UFJoin.php (copied from Tag ) but it doesn't do the third option of converting an id-indexed one back to a sequentially indexed. (My tired brain is trying to turn 3 possibilies into a boolean)


Do you have examples when you found easier to get the list indexed with ids ?

In my case, I never used the key, beside when I'm fetching a contact knowing its id ($the_variable_I_want = $result[ $params['id'] ] ), that I would happily replace by a $the_variable_I_want = array_shift($result) As I know that I will never get more than one entity that has id = X.

Oh, and for what I recall when I was studying math and binary, It's called base 2 for a reason indeed ;)

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: create_success ... again
January 24, 2011, 06:22:49 pm
Ok - so I can't think of an example but would prefer to keep default php behaviour unchanged to keep things a little simpler.

My main point is that some functions receive the array already indexed by ids from the BAO functions they call whereas others don't. I think that civicrm_create_success should cope with these being passed in as they come back from the BAO function. Xavier - you moved this functionality out of the Tag.php & I think that is correct - it's not too complicated to make civicrm_create_success accept both sequentially numbered & id numbered arrays & split our either or depending on $variables



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) »
  • create_success ... again

This forum was archived on 2017-11-26.