3

If I want to find all contacts without a birth date, I can do this with the api:

$result = civicrm_api3('Contact', 'get', array(
  'sequential' => 1,
  'birth_date' => array('IS NULL' => 1),
));

If I want to find all contacts with a birth date before a given date, it works like this:

$result = civicrm_api3('Contact', 'get', array(
  'sequential' => 1,
  'birth_date' => array('<=' => "1960-01-01"),
));

Now suppose I want to both the contacts without a birth date and the contacts born before a particular date, can I do that in one API call?

4

Barring some undocumented feature (which are rife in the API, so someone please correct me), I think you'll have to merge the two. However, since you're dealing with "get" calls, it's straightforward to merge the two results.

First, you'll want to get rid of the sequential parameter; without it, you'll get arrays of values keyed by ID. Then, you'll have a pretty simple situation:

$result = civicrm_api3('Contact', 'get', array(
  'birth_date' => array('IS NULL' => 1),
));

$result2 = civicrm_api3('Contact', 'get', array(
  'birth_date' => array('<=' => "1960-01-01"),
));

$combined = array_merge($result['values'], $result2['values']);

The result will have only one row per contact. Although obviously there's no overlap between people with no birth date and people with birth dates prior to 1960, you could use the same principle for merging potentially overlapping groups since the array key is the ID.

  • Thank you for this clean solution. A problem with it is that you cannot use API-chaining. But it's not too hard to work around. – johanv Aug 13 '15 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.