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) »
  • Updating via CREATE
Pages: 1 [2]

Author Topic: Updating via CREATE  (Read 2700 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Updating via CREATE
February 06, 2011, 03:43:15 pm
hmm - why is there reference to ajax in this?

civicrm_api/crmAPI/ajax

we're not talking about REST only stuff are we?

To be honest I'm most inclined just to remove the & before params on create and / or to edit the functions so that they construct a $values off the params (which some do).

I suppose I just haven't fully 'got with' the new way of doing stuff yet & am still thinking v2 + standardisation new a whole new generation of API
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: Updating via CREATE
February 06, 2011, 03:44:54 pm
Tell you what - once you've committed I promise to get the AJAX stuff running & see the new coolness
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: Updating via CREATE
February 07, 2011, 12:13:22 am
Hi,

Let's leave the api as compatible as possible (ie. not a lot). Whatever was doing the create is mostly good enough, and if the create with an id in param was happily replacing the existing fields with empty ones, so be it (let's hide all the issues about &$params and say that's for compatibility reasons ;)

For v3, let's decide that the preferred way is to call the api (php version)

civicrm_api (entity,action,param)

and the preferred way for the ajax is
/civicrm/ajax/rest?entity=xx&action=yyy (instead of fnName=civicrm_entity_action)

And we introduce a new action "update", that doesn't have any civicrm_entity_update, but does your magic of fetching the existing data with a civicrm_entity_get, merge the arrays and call civicrm_entity_create

This can sit in the civicrm_api only. We solved the &$params, didn't change anything on the existing functions and introduced a new action on 30+ entities with a handful of lines of code ;)

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: Updating via CREATE
February 07, 2011, 12:16:47 am
Quote
This can sit in the civicrm_api only. We solved the &$params, didn't change anything on the existing functions and introduced a new action on 30+ entities with a handful of lines of code ;)

You do sell it rather well.

I think there should be an option as to what to do if the id isn't value - default = return error, option = create new (which means that I could cheat & use it as my default function whether or not I'm passing in a valid ID......)
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: Updating via CREATE
February 07, 2011, 12:40:11 am
Hi,

was looking at some code of "update or create".

That was on v2:

Code: [Select]
$paramsSearch = array (search criterias);
$params = array (stuff I want to save);

$result = civicrm_contact_get ($paramsSearch);
if (!$result[is_error]) {
  $params [contact_id] = $result[I never know what to put sequential I miss you][contact_id];
}
civicrm_contact_ create ($params);

With the v3:

Code: [Select]
$action= 'create';
$paramsSearch = array (search criterias, sequential => 1);
$params = array (stuff I want to save);

$result = civicrm_api ('contact','get', $paramsSearch);
if (!$result[is_error]) {
  $params [contact_id] = $result[0][contact_id]; // we know that 0 is the first one, no matter its id
  $action= 'update';
}
civicrm('contact',$action,$params);

I'd argue that it doesn't add a lot of lines and I'd go as far as saying that's clearer that you modify. Got a use case where it would be more complicated to set the $action string ?
-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: Updating via CREATE
February 07, 2011, 01:09:26 am
After a chat with eileen,

civicrm_api (whatever, 'update', $params)

has a new option:

$params['createOnFail'] = true (by default false)

If that case, if the id is invalid/doesn't match a record, it removes the id and calls civicrm_api(whatever,'create', $params)

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

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Updating via CREATE
February 07, 2011, 06:53:53 am

can you explain why we need 'createOnFail'

seems like a crude hack. if an id is passed in which is invalid, most of the times something is wrong higher up the chain

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Updating via CREATE
February 07, 2011, 08:50:10 am
Hi,

Something Eileen needed to integrate with views.

Don't have a strong opinion for nor against... bit like ON DUPLICATE KEY UPDATE in mysql insert, as long if it's not the default...

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: Updating via CREATE
February 13, 2011, 11:54:20 pm
I've implemented civicrm_api (entity,'modify',$param) that

(entity,get), merge the param and what's in the db, and (entity,create)

The issue so far is that the id is sometimes id, sometimes entity_id, sometimes id, and some returns have a different format.

I'm taking the convention that id is the name of the id. I suppose we need to modify the create & get apis to be sure the id is always id.

Let me know if you have a better id(ea)
X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

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

This forum was archived on 2017-11-26.