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) »
  • Finding Contacts based on CustomValues
Pages: [1]

Author Topic: Finding Contacts based on CustomValues  (Read 574 times)

kengle3

  • I’m new here
  • *
  • Posts: 16
  • Karma: 0
Finding Contacts based on CustomValues
January 06, 2012, 12:08:38 pm
In some code I'm writing, I'm trying to find a way to find contacts in the database based upon CustomValues.

For example, lets say I want to find all the contacts where a CustomValue "X" is equal to 10.

At the moment, I've solved the problem by code similar to below (this isn't my actual code, just an example of how I solved the problem. There may be typos):

$param = array('contact_type' => 'Individual', 'version' => 3,);
$results = civicrm_api( 'contact','get',$param );

foreach($results as $individual)
{

$cid = $individual['contact_id'];
$params = array('entity_id' => $cid, 'version' => 3, 'return.myCustomValue:x' => 1,);
$customValue = civicrm_api('custom_value', 'get', $params);
$x = $customValue['values'][<index of X>];

if($x == 10)
doSomething($cid);

}

However this seems a bit backwards to me. As I currently understand it, the above code performs at least N+1 queries to the database which doesn't seem very efficient when all I care about are contacts where X is equal to 10.

What seems would make the most sense, would be to pull all the entries where 'myCustomValue:x' is equal to 10. However from my understanding of the API is a contactID is required to get CustomValues, and CustomValues can only be pulled individually.

Essentially what I need seems to be the inverse of the CustomValue get API function. I don't have the contactID, but I have the CustomValue I'm searching for.

Is there a way to pull CustomValues without a specific contactID?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Finding Contacts based on CustomValues
January 06, 2012, 12:43:36 pm
The contact search is based on the same query object as the advanced search so it may be possible to feed in $params['custom_10'] => '10' (Where 10 is both the search value & your custom field 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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Finding Contacts based on CustomValues

This forum was archived on 2017-11-26.