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) »
  • fetch multiple contacts by known ids
Pages: 1 [2]

Author Topic: fetch multiple contacts by known ids  (Read 4281 times)

Jeremy Proffitt

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 2
    • Mobius New Media
  • CiviCRM version: 4.4.x
  • CMS version: Joomla 2.5.x/3.x / D7
  • MySQL version: 5.1.x
  • PHP version: 5.3.10+, 5.4.x
Re: fetch multiple contacts by known ids
March 08, 2013, 04:20:17 am
I did in fact just have the BAO evaluate the id or contact_id param with is_array() and make the clause "IN" accordingly. In the PHP api call, a Contact Get will now simply take an array in addition to an integer. I don't think I'm following how you're suggesting using the + operator.
Jeremy Proffitt
Mobius New Media
IRC: JP_EzoD

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: fetch multiple contacts by known ids
March 08, 2013, 06:23:59 am
Is this already in the core? Would be good to extend to the ajax/rest api too, with a id=1,43,49 syntax being converted to array (1,43,39)

Do you think you could tackle it?

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

Jeremy Proffitt

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 2
    • Mobius New Media
  • CiviCRM version: 4.4.x
  • CMS version: Joomla 2.5.x/3.x / D7
  • MySQL version: 5.1.x
  • PHP version: 5.3.10+, 5.4.x
Re: fetch multiple contacts by known ids
March 08, 2013, 01:29:17 pm
I'm happy to make the change, but I don't see exactly where that is being handled. I also don't know how that reconciles with Eileen's comments, since I wasn't quite clear what she was getting at.
Jeremy Proffitt
Mobius New Media
IRC: JP_EzoD

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: fetch multiple contacts by known ids
March 10, 2013, 01:16:01 pm
This was the earlier discussion around adding other operators

http://forum.civicrm.org/index.php/topic,22118.msg92650.html#msg92650

The intention was that all fields should eventually support either a value or an array keyed by the operator (this currently works for some eg. address,  batch, campaign, contribution page, custom field, custom group etc on beyond the letter c).

But it doesn't work for others - e.g. contact, contribution pledge

If introducing new support for it then ideally it would follow the pattern e.g

civicrm_api('contact', 'get', array('version' => 3, 'id' => array('IN', array(1,2,3,4,)));

OR

civicrm_api('contact', 'get', array('version' => 3, 'id' => array('BETWEEN', array(1,4,)));

OR

civicrm_api('contact', 'get', array('version' => 3, 'id' => array('<', 5));

You obviously found where that was being done @ the api layer - but that isn't being accessed from the BAO layer. Ideally moving the relevant part of the function to sit on the BAO layer (or in CRM_Core_DAO) & calling it from BAO & api would be the approach.

Whatever syntax is added I wouldn't introduce it into a production site without ensuring there is a unit test to safeguard it in future.
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

Jeremy Proffitt

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 2
    • Mobius New Media
  • CiviCRM version: 4.4.x
  • CMS version: Joomla 2.5.x/3.x / D7
  • MySQL version: 5.1.x
  • PHP version: 5.3.10+, 5.4.x
Re: fetch multiple contacts by known ids
March 11, 2013, 06:04:29 am
Ok. Based on that, I'll need to rework what I was doing, as the patch in CRM-12051 for CRM_Contact_BAO_Query only checks to see if the id param is passed, and checks to see if it is an array. If is_array is true, then it assumes that the array is an array of integers and implodes to a list, and creates the IN statement accordingly. This is actually being done in CRM_Contact_BAO_Query::whereClause(), but it would seem an additional method would be in order.

If we're going to the trouble to make all of the operators available, it would make sense to actually key the array, so that you could potentially pass multiple conditions. i.e. --

Instead of

civicrm_api('contact', 'get', array('version' => 3, 'id' => array('IN', array(1,2,3,4,)));
-> creates condition 'id IN (1,2,3,4)'

Use

civicrm_api('contact', 'get', array('version' => 3, 'id' => array('BETWEEN' => array(1,4,), '!=' => 3)));
-> creates conditions 'id BETWEEN 1 AND 4 AND  id != 3'


That still didn't answer my other question about where the input from an AJAX request is interpreted, since the input will have be converted into the array format we're discussing.
« Last Edit: March 11, 2013, 06:22:20 am by Jeremy Proffitt »
Jeremy Proffitt
Mobius New Media
IRC: JP_EzoD

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: fetch multiple contacts by known ids
March 11, 2013, 08:05:52 am
Quote from: Jeremy Proffitt on March 11, 2013, 06:04:29 am

That still didn't answer my other question about where the input from an AJAX request is interpreted, since the input will have be converted into the array format we're discussing.

On the latest versions, you can json encode the param and stick it into &json=

json={"id":{"in"=[1,2,3,4]}

I know I used it as a post param (when chaining calls), not 100% sure it works for a get one, but should
-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: fetch multiple contacts by known ids
March 11, 2013, 12:57:31 pm
Quote
If we're going to the trouble to make all of the operators available, it would make sense to actually key the array, so that you could potentially pass multiple conditions. i.e. --

I think what you are suggesting would be supported by the DAO based functions already based on this.

It seems it would use 'AND' and 'OR' would still be unavailable - but if we start looking @ a syntax for 'OR' we'd need to not limit it to 1 field so consisentency on the existing syntax probably matters more.

I've put a mockup of how we might restructure the existing function onto the DAO so it is more generic

https://github.com/eileenmcnaughton/civicrm-core/commit/56d5716e7f9cc1e5f81e6f8f63b3982bff640962
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

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: fetch multiple contacts by known ids
July 11, 2013, 06:00:49 am
Quote from: xavier on March 11, 2013, 08:05:52 am

On the latest versions, you can json encode the param and stick it into &json=

json={"id":{"in"=[1,2,3,4]}

I know I used it as a post param (when chaining calls), not 100% sure it works for a get one, but should

I think this is not valid json. It should be

Code: [Select]
json={"id":{"in":[1,2,3,4]}}
right? But even with the correct syntax an error message "no such field" is returned. Is there something wrong with the parameter or is it not possible at the moment, to request contacts (or other entities) by multiple ids?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: fetch multiple contacts by known ids
July 11, 2013, 06:51:25 am

Quote
I think this is not valid json. It should be

Code: [Select]
json={"id":{"in":[1,2,3,4]}}
right?

yes, thanks.

Quote
But even with the correct syntax an error message "no such field" is returned. Is there something wrong with the parameter or is it not possible at the moment, to request contacts (or other entities) by multiple ids?

json is possible, but the underlying api doesn't support multiple ids query yet.
As soon as we receive your patch+test, you'd get it working from ajax, smarty, rest and php ;)

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

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: fetch multiple contacts by known ids
July 11, 2013, 07:19:29 am
No problem, I will instruct my other unit test crm api skilled personality to do that ;)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: fetch multiple contacts by known ids
July 12, 2013, 10:02:45 am
Cool, if either of you is around in london on oct 3-4, you should come and present the result at civicon.

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

Laryn

  • I post frequently
  • ***
  • Posts: 192
  • Karma: 4
    • CEDC
  • CiviCRM version: 4.6.x
  • CMS version: Drupal 7
Re: fetch multiple contacts by known ids
August 27, 2013, 09:33:13 am
To tie this up a bit for future searchers -- talked to Eileen on IRC last night and she had just committed this for 4.4:

https://github.com/civicrm/civicrm-core/pull/1517
CEDC...social justice by design

*Get support on the new CiviCRM help site. /laryn

Pages: 1 [2]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • fetch multiple contacts by known ids

This forum was archived on 2017-11-26.