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) »
  • Dashboard API updating instead of creating
Pages: [1]

Author Topic: Dashboard API updating instead of creating  (Read 390 times)

brandonbryan12

  • I’m new here
  • *
  • Posts: 10
  • Karma: 2
  • CiviCRM version: 4.4
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.5
Dashboard API updating instead of creating
November 26, 2014, 01:49:16 pm
I am writing a Drupal module and am using the create method on a Dashboard using the API explorer.

The array passed in does not contain an id, but updates an instance based off of matching params.

Whenever there is a url param, the entity instance that matched that param will be the one that is edited. But if I try to create with only a matching title param, it updates id 1. Only by themselves some other params will create a new instance.

This hasn't been happening the whole time. Does anyone have ideas?

(I'm using CiviCRM 4.5)

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Dashboard API updating instead of creating
November 28, 2014, 10:33:15 am
In APIv3, the norm is to do updates based on matching IDs; if you want to match/update based on a different field, then it normally requires multiple calls -- e.g. do a "get" followed by "create". There's some options which are useful for doing a get+create in a more concise fashion:

  • API chaining -- Issue a 'get' call with a nested 'create'
  • options.match -- http://wiki.civicrm.org/confluence/display/CRMDOC/API+changes#APIchanges-4.4.0:%27create%27and%27replace%27actionsaccept%27options.match%27or%27options.match-mandatory%27

The behavior you point out in the Dashboard API isn't normative; it's more adhoc. This is not entirely unusual -- it often happens with a story like this:

  • Developer #1 creates a BAO function (e.g. CRM_Core_BAO_Dashboard::create or CRM_Core_BAO_Dashboard::addDashlet) for a specific use-case. The function has some special behavior which makes sense for that use-case.
  • A few months later, developer #2 creates an API. The API builds on top of the existing BAO function, and developer #2 assumes that the BAO function works "normally" (or assumes that anything unusual is good or exists for a valid reason).
  • The assumption may or may not be valid. If the assumption is wrong, then eventually someone realizes that the API is weird.

The most authoritative way to find out what's going on in this situation is to read the BAO code.

  • https://github.com/civicrm/civicrm-core/blob/4.5/CRM/Core/BAO/Dashboard.php#L50
  • https://github.com/civicrm/civicrm-core/blob/4.5/CRM/Core/BAO/Dashboard.php#L411

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

This forum was archived on 2017-11-26.