I have created a API. In this a simple select query is running and data is displaying as JSON format

But I don't how to handle / pass all the conditions to the select query

Parameter = 1 or Parameter <= 1 or Parameter => 1 or Parameter != 1 or,...

enter image description here

Currently my query is as below

Select col1,col2,col3 from civicrm_table

I want to build my select query as

Select col1,col2,col3 from civicrm_table where condition

Here condition may be any one from the list

Is there a simplest way to handle this in Civi API

My API code


 * Membershipperiod.Get API specification (optional)
 * This is used for documentation and validation.
 * @param array $spec description of fields supported by this API call
 * @return void
 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards
function _civicrm_api3_membershipperiod_Get_spec(&$spec) {
//  $spec['magicword2']['api.required'] = 1;
  $spec['membership_id']['api.required'] = 1;

 * Membershipperiod.Get API
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
function civicrm_api3_membershipperiod_Get($params) {

    foreach($params['membership_id'] as  $key=>$val)
         $val="p.membership_id $key $val";
$condition=" and $val";

  $query = "SELECT c1.display_name, m.contact_id, c.total_amount,p.membership_id 
FROM civicrm_membership m
RIGHT JOIN civicrm_membership_payment p ON m.id = p.membership_id
INNER JOIN civicrm_contribution c ON c.id = p.contribution_id
INNER JOIN civicrm_contact c1 ON c1.id = m.contact_id $condition";

//    $result_array[]=$params['membership_id'];
  $dao = CRM_Core_DAO::executeQuery($query);
  while ($dao->fetch()) {

    $result_array[]=array("contact_id" => $contact_id,"membership_id" =>$membership_id, "name" => $display_name, "amount_paid" => $total_amount);

    // Spec: civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL)
    return civicrm_api3_create_success($returnValues, $params, 'NewEntity', 'NewAction');



class CRM_History_BAO_MembershipPeriod extends CRM_History_DAO_MembershipPeriod {

   * Create a new MembershipPeriod based on array-data
   * @param array $params key-value pairs
   * @return CRM_History_DAO_MembershipPeriod|NULL
  public static function create($params) {
    $className = 'CRM_History_DAO_MembershipPeriod';
    $entityName = 'MembershipPeriod';
    $hook = empty($params['id']) ? 'create' : 'edit';

    CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
    $instance = new $className();
    CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);

    return $instance;
  } */


1 Answer 1


It looks like you pretty much have it almost done from your screenshot. The where conditions are in the parameters, you have membership_id selected, and you can choose an operator and value for each of the parameters.

Regarding the SELECT col1, col2, col3, if you want to limit to only certain fields you can do that with the "Fields to return" box.

  • I want to create where condition based on the drop down values and input parameters Commented Jul 7, 2017 at 3:55
  • I'm not sure I understand, is this for a custom api in an extension for CiviCRM that you've created?
    – mickadoo
    Commented Jul 7, 2017 at 9:31
  • I have created this API using CIVIX, in API function I am wring my own query where I have to pass the conditions dynamically; Commented Jul 7, 2017 at 13:01
  • Do you see your custom entity in the API explorer with the fields it has in the parameter list? If you do and you're not seeing the selections reflected in the query are you calling the basic functions from inside your custom api functions, you can see examples here
    – mickadoo
    Commented Jul 7, 2017 at 13:05
  • I have placed my Code in the question. Could you please check and guide Commented Jul 7, 2017 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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