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 using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
Pages: [1]

Author Topic: API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)  (Read 2952 times)

Laryn

  • I post frequently
  • ***
  • Posts: 192
  • Karma: 4
    • CEDC
  • CiviCRM version: 4.6.x
  • CMS version: Drupal 7
API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
April 16, 2013, 09:58:48 pm
What I'd like to do is use the API to pull activities for a particular contact by date range (e.g. the last month by default, with a selection to choose earlier months).

Colemanw was giving me some direction on IRC:
Quote
<colemanw> laryn: the api does support some operators like >, <, LIKE, BETWEEN, etc ... should be in the api docs somewhere

Unfortunately, I haven't had much luck in my searching to find out appropriate syntax. I've found the following:
  • Civi4.1 Wiki entry mentioning "of using 'LIKE', IN, BETWEEN, NOT BETWEEN, NOT LIKE & is_current" such as:
Code: [Select]
$params = array(
  'version' => 3,
  'relationship_type_id' => array(
      'BETWEEN' => array(
          '0' => 33,
          '1' => 35,
        ),
    ),
);

  $result = civicrm_api( 'relationship','get',$params );
    [/li]
  • An entry on JIRA which may indicate this functionality isn't included?

So far I've tried the following, but am not getting results:
Code: [Select]
    $params = array(
      'activity_date_time' => array(
          'BETWEEN' => array(
          '0' => '03/01/2013',
          '1' => '03/31/2013',
          ),
      ),     
      'version' => 3,
      'sequential' => 1,
      'source_contact_id' => $source_contact_id,
      'activity_type_id' => $activity_type_id,
      'sort' => 'activity_date_time DESC',
    );

I've also tried modifying it based on the fields in the activity search page (where you can search by range):
Code: [Select]
    $params = array(
      'activity_date_low' => '03/01/2013',
      'activity_date_high' => '03/31/2013',
      'version' => 3,
      'sequential' => 1,
      'source_contact_id' => $source_contact_id,
      'activity_type_id' => $activity_type_id,
      'sort' => 'activity_date_time DESC',
    );
This last code returns all results without any date filtering, while the first doesn't return any results.

Any guidance or further direction in my search would be greatly appreciated! I will try to update the Wiki with any discoveries... I should probably note I haven't had a chance to update the site that I am working on this with to 4.3 yet (I believe it is at 4.2.8 right now).
CEDC...social justice by design

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

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
April 17, 2013, 12:55:38 pm
I would try filtering on simpler data type first, like an integer, to make sure the operators are working properly. If that is working, then I might try different date formats for the arguments. Sorry I haven't got better advice since I haven't used this myself. FYI Coleman has put the query to the API team via email.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
April 17, 2013, 11:17:20 pm
The stored example suggests to me that the date is not going through 'strtotime' & you need to use dates that mysql likes eg.

http://svn.civicrm.org/civicrm/branches/v4.2/api/v3/examples/Activity/DateTimeHigh.php


/*
 demonstrates _high filter (at time of writing doesn't work if contact_id is set
 */
function activity_get_example(){
$params = array(
  'source_contact_id' => 17,
  'version' => 3,
  'filter.activity_date_time_high' => '20120101000000',
  'sequential' => 1,
);

  require_once 'api/api.php';
  $result = civicrm_api( 'activity','get',$params );

  return $result;
}
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

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 using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
April 18, 2013, 12:43:39 am
Would be really nice if the API is fixed so it does not rely on dates that MySQL likes? Would that be something you would be willing and able to undertake Laryn?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
April 19, 2013, 02:57:39 pm
It may not be the reason - I also just noticed filter. in the one param below
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

Laryn

  • I post frequently
  • ***
  • Posts: 192
  • Karma: 4
    • CEDC
  • CiviCRM version: 4.6.x
  • CMS version: Drupal 7
Re: API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
April 25, 2013, 03:23:27 pm
I will do some further digging and report back -- thanks for the ideas!
CEDC...social justice by design

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

Laryn

  • I post frequently
  • ***
  • Posts: 192
  • Karma: 4
    • CEDC
  • CiviCRM version: 4.6.x
  • CMS version: Drupal 7
Re: API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)
June 04, 2013, 12:22:47 pm
Just to follow up -- Eileen gets the gold star! The example she dug up was very instructive and that is the format the date needs to be in.
CEDC...social justice by design

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API get using 'LIKE' or 'BETWEEN' (e.g. activity by date range)

This forum was archived on 2017-11-26.