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) »
  • api get query for empty fields & wildcards?
Pages: [1]

Author Topic: api get query for empty fields & wildcards?  (Read 941 times)

karlis

  • I’m new here
  • *
  • Posts: 5
  • Karma: 0
  • CiviCRM version: 4.1.1
  • CMS version: drupal 7.12
  • MySQL version: 5.1.63
  • PHP version: 5.2.17
api get query for empty fields & wildcards?
September 27, 2012, 11:34:59 pm
Is there any way to use the API to do searches for empty records or wildcard/LIKE searches?

What I'm trying to do run an update on about 25% of our contact records (about 1000 records) so that one of the custom fields gets updated from hook_civicrm_post. If I try anything more than rowcount 100 it maxes out the memory usage. So I have to run it with limits and offsets.

Here's what I'm currently using:

civicrm_api('Contact','Get',array('contact_type' => 'Individual','custom_7' => '','version' =>3,'sort'=>'contact_a.id ASC','rowCount' => '100','offset'=>"$offset"));

 What I would like is to be able SQL-style search where custom_7 = '', but can that be done with the API?  The above statement seems to return all records, not the ones with the custom_7 field empty.  I can't find any documentation describing how to do this.

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: api get query for empty fields & wildcards?
September 28, 2012, 05:53:23 am
Try the API explorer, do you know where it is? Also, you should be able to use limit and offset in the API using this structure:
Code: [Select]
$params = array( ' option_group_id' => 1, ' version'  => 3, ' options'  => array( ' sort'  => ' label DESC' , ' limit'  => 1 ) );
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

karlis

  • I’m new here
  • *
  • Posts: 5
  • Karma: 0
  • CiviCRM version: 4.1.1
  • CMS version: drupal 7.12
  • MySQL version: 5.1.63
  • PHP version: 5.2.17
Re: api get query for empty fields & wildcards?
September 28, 2012, 12:20:43 pm
I had seen the API explorer but hadn't really played with it.  I think the explorer page could use some on-page documentation or encouragement, right now it looks like something I would make :) I think it would also help if the explorer displayed the resulting SQL statement as well.

Anyway  I have so far discovered that you can use wildcards just like in MYSQL, so 'custom_7=Van%' and 'custom_7=Vancouver' will both find all records with custom_7 field = Vancouver.  'display_name' seems to assume a wildcard, so that 'display_name=David' will find records for "David Alpha" and "David Beta".  Go figure, inconsistent. Maybe some other fields assume a wildcard as well.

I still haven't figured out how to query for records with empty fields. If I try 'custom_7=' it seems to work the same way as 'custom_7=%', that is, returns all records.  'custom_7=''' obviously shows records with the field containing two single quotes.

Any suggestions?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: api get query for empty fields & wildcards?
September 29, 2012, 07:27:02 am

Can you try using the search builder and using IS NULL or IS EMPTY (i.e. blank string or NULL)

I dont think the api (or advanced search) allows you to send this info to the query builder

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • api get query for empty fields & wildcards?

This forum was archived on 2017-11-26.