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) »
  • Ability of API to "join" tables
Pages: [1]

Author Topic: Ability of API to "join" tables  (Read 975 times)

seGAP

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Drupal 7
  • MySQL version: 5.0
  • PHP version: 5.2
Ability of API to "join" tables
March 29, 2013, 04:42:12 am
Hi,

Although I've found documentation of chaining the API, the examples appear to be for Creates, rather than Gets.

Is it possible to use the API to do a single Get for "All current Memberships for Households in a particular State"? i.e. the equivalent of a Join between the Membership and Contact tables.

Or would we have to run a Get for the Memberships, and then a second Get to filter the results down for the Households? (which would appear likely to be much slower, and complex)

Or do we have to resort to direct SQL, in order to achieve the Join? (which is something we'd prefer to avoid).

Thanks, 
Bryan

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Ability of API to "join" tables
March 29, 2013, 06:16:51 am
You could use the API chaining for Gets to, so you should be albe to have a Get for the Memberships and the households. You can not join the two gets in one API call.
I would certainly not recommend doing the direct SQL for all the obvious reasons. If you feel that the API is insufficient you could use

Code: [Select]
$mySelect = "SELECT fielda, fieldb FROM table1 JOIN table2 ......";
$daoResult = CRM_Core_DAO::executeQuery( $mySelect);
while ($daoResult->fetch() ) {
   $field = $daoResult-fielda;
}

so that you at least use the CiviCRM DAO.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

seGAP

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Drupal 7
  • MySQL version: 5.0
  • PHP version: 5.2
Re: Ability of API to "join" tables
March 29, 2013, 03:45:48 pm
Thanks Erik,

I'm still hoping to use Chained Gets, but I can't find any examples of the syntax... all the code samples seem to be for Create.

The documentation for Chaining suggests that the Id value from the first API call will be passed into the second API as entity_id.

That makes sense for a Create chain, but it's not clear to me how this will work when the first Get API returns multiple rows... which is why I'm looking for code samples.

Do you know where I can find samples for chained Gets?

Thanks 
Bryan

seGAP

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Drupal 7
  • MySQL version: 5.0
  • PHP version: 5.2
Re: Ability of API to "join" tables
March 30, 2013, 12:04:41 am
OK, cancel that request. I should have just tried it with Get replacing the Create, instead of worrying if it would work, which of course it does.

For the record (at least until someone can point out some more formal documentation on Chaining Gets), the Chained API call returns an array of results from the first Get, with each of the Keys being used as input to the second Get to produce another array of results.

The Key from the first array can therefore be cross-referenced by PHP to the second array (effectively as a Foreign Key).

Sorry, if this is blatantly obvious to most, but it took a while for me to get my head around the Chaining process.
Bryan

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Ability of API to "join" tables
March 30, 2013, 03:38:12 am
Glad it works, and thanks for sharing!! Good to find out how the documentation is read, it will help us all to improve.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Ability of API to "join" tables
March 30, 2013, 04:25:15 am
<quote>
For the record (at least until someone can point out some more formal documentation on Chaining Gets), the Chained API call returns an array of results from the first Get, with each of the Keys being used as input to the second Get to produce another array of results.</quote>

Could you amend the wiki to clarify? seems we failed detailing it.

Please keep in mind that the chained api means that for each "master" entity returned from the initial get, it will generate the chained request, so if you have 20 results, you will have 21 api calls.

Can you do the request you want directly in the advanced search? if it's is the case, a
<code>
civicrm_api("contact","get",array (contact_type=>Household, whatever_filter_on_membership => the thing you want
return=array("sortname",...)
)
</code>
should work
-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) »
  • Ability of API to "join" tables

This forum was archived on 2017-11-26.