Answers.Body: <p>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.</p>
  
  <p>Regarding the <code>SELECT col1, col2, col3</code>, if you want to limit to only certain fields you can do that with the "Fields to return" box.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-07T03:55:17.237Z
Answers.Text: I want to create where condition based on the drop down values and input parameters
Answers.UserDisplayName: ManiMuthuPandi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-07T09:31:55.927Z
Answers.Text: I'm not sure I understand, is this for a custom api in an extension for CiviCRM that you've created?
Answers.UserDisplayName: mickadoo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-07T13:01:56.407Z
Answers.Text: I have created this API using CIVIX, in API function I am wring my own query where I have to pass the conditions dynamically;
Answers.UserDisplayName: ManiMuthuPandi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-07T13:05:34.773Z
Answers.Text: 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](https://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards)
Answers.UserDisplayName: mickadoo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-07T17:37:46.253Z
Answers.Text: I have placed my Code in the question. Could you please check and guide
Answers.UserDisplayName: ManiMuthuPandi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-07T18:35:04.957Z
Answers.Text: Have you tried `return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);`  instead of your custom implementation in your `civicrm_api3_membershipperiod_Get` function?
Answers.UserDisplayName: mickadoo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-08T08:15:14.157Z
Answers.Text: When I am trying I am geting this error  `[08-Jul-2017 13:42:49 Asia/Kolkata] PHP Fatal error:  Class name must be a valid object or a string in /opt/lampp/htdocs/drupal/sites/all/modules/civicrm/Civi/API/SelectQuery.php on line 101`
Answers.UserDisplayName: ManiMuthuPandi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-08T10:10:40.937Z
Answers.Text: Ah, so you haven't created a new entity, this is just some API that returns something based on existing tables? Your approach in the code seems fine then, if you want to add a where clause you'll have to do it all manually though, reading from the params and inserting the where clause in the query you send to `executeQuery`
Answers.UserDisplayName: mickadoo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-08T10:36:09.963Z
Answers.Text: But I have a table named `civicrm_membershipperiod` already
Answers.UserDisplayName: ManiMuthuPandi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-08T23:10:09.550Z
Answers.Text: Then why does your code not select from that table? The error you're getting seems to point to a lack of a BAO. Have you also used civix to generate the entity files?
Answers.UserDisplayName: mickadoo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-09T05:34:03.040Z
Answers.Text: Yes, I have used civix for creating this entity and followed the steps from this link https://docs.civicrm.org/dev/en/master/extensions/civix/. After creating entity I have copied only DAO file from the development version. I have added BAO file in my question
Answers.UserDisplayName: ManiMuthuPandi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-09T07:10:57.993Z
Answers.Text: I found the issue; It my mistake; Created extension as `MembershipPeriod` and table as `civicrm_membershipperiod`. Now by creating extension as `Membershipperiod` solved the problem
Answers.UserDisplayName: ManiMuthuPandi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-09T10:57:47.607Z
Answers.Text: I wasn't so much help, but great to hear you got it working
Answers.UserDisplayName: mickadoo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-06T19:42:02.047Z
Answers.LastActivityDate: 2017-07-06T19:42:02.047Z
Answers.UserDisplayName: mickadoo
Body: <p>I have created a API. In this a simple select query is running and data is displaying as JSON format</p>
  
  <p>But I don't how to handle / pass all the conditions to the select query</p>
  
  <p>Parameter = 1 or 
  Parameter &lt;= 1 or 
  Parameter => 1 or 
  Parameter != 1 or,...</p>
  
  <p><a href="https://i.sstatic.net/xWF6X.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xWF6X.png" alt="enter image description here"></a></p>
  
  <p>Currently my query is as below</p>
  
  <pre><code>Select col1,col2,col3 from civicrm_table
  </code></pre>
  
  <p>I want to build my select query as</p>
  
  <pre><code>Select col1,col2,col3 from civicrm_table where condition
  </code></pre>
  
  <p>Here condition may be any one from the list</p>
  
  <p>Is there a simplest way to handle this in Civi API</p>
  
  <p><strong>My API code</strong></p>
  
  <pre><code>&lt;?php
  
  /**
   * 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(&amp;$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) {
  
  $condition="";
  if($params['membership_id'])
  {
    if(is_array($params['membership_id']))
      {
      foreach($params['membership_id'] as  $key=&gt;$val)
           $val="p.membership_id $key $val";
      }
  else
  $val="p.membership_id=".$params['membership_id'];
  $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=array();
  //    $result_array[]=$params['membership_id'];
    $dao = CRM_Core_DAO::executeQuery($query);
    while ($dao-&gt;fetch()) {
      $display_name=$dao-&gt;display_name;
      $contact_id=$dao-&gt;contact_id;
      $total_amount=$dao-&gt;total_amount;
      $membership_id=$dao-&gt;membership_id;
  
      $result_array[]=array("contact_id" =&gt; $contact_id,"membership_id" =&gt;$membership_id, "name" =&gt; $display_name, "amount_paid" =&gt; $total_amount);
     }
  
  $returnValues=$result_array;
      // Spec: civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL)
      return civicrm_api3_create_success($returnValues, $params, 'NewEntity', 'NewAction');
  
  }
  ?&gt;
  </code></pre>
  
  <p><strong>BAO</strong></p>
  
  <p>
  
  <pre><code>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();
      $instance-&gt;copyValues($params);
      $instance-&gt;save();
      CRM_Utils_Hook::post($hook, $entityName, $instance-&gt;id, $instance);
  
      return $instance;
    } */
  
  }
  </code></pre>
  
Tags: <api><webform-civicrm>
Title: How to handle conditions in API
ThreadText: # How to handle conditions in API
  
  (Asked by: username_0 on 2017-07-06)
  
  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](https://i.sstatic.net/xWF6X.png)](https://i.sstatic.net/xWF6X.png)
  
  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**
  
  ```
  <?php
  
  /**
   * 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) {
  
  $condition="";
  if($params['membership_id'])
  {
    if(is_array($params['membership_id']))
      {
      foreach($params['membership_id'] as  $key=>$val)
           $val="p.membership_id $key $val";
      }
  else
  $val="p.membership_id=".$params['membership_id'];
  $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=array();
  //    $result_array[]=$params['membership_id'];
    $dao = CRM_Core_DAO::executeQuery($query);
    while ($dao->fetch()) {
      $display_name=$dao->display_name;
      $contact_id=$dao->contact_id;
      $total_amount=$dao->total_amount;
      $membership_id=$dao->membership_id;
  
      $result_array[]=array("contact_id" => $contact_id,"membership_id" =>$membership_id, "name" => $display_name, "amount_paid" => $total_amount);
     }
  
  $returnValues=$result_array;
      // Spec: civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL)
      return civicrm_api3_create_success($returnValues, $params, 'NewEntity', 'NewAction');
  
  }
  ?>
  
  ```
  
  **BAO**
  
  ```
  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();
      $instance->copyValues($params);
      $instance->save();
      CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
  
      return $instance;
    } */
  
  }
  
  ```
  
  **Tags:** api, webform-civicrm
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-07-06. Score: 2 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_0**: I want to create where condition based on the drop down values and input parameters  
  > **username_1**: I'm not sure I understand, is this for a custom api in an extension for CiviCRM that you've created?  
  > **username_0**: I have created this API using CIVIX, in API function I am wring my own query where I have to pass the conditions dynamically;  
  > **username_1**: 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](https://wiki.civicrm.org/confluence/display/CRMDOC/API+Architecture+Standards)  
  > **username_0**: I have placed my Code in the question. Could you please check and guide  
  > **username_1**: Have you tried `return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);`  instead of your custom implementation in your `civicrm_api3_membershipperiod_Get` function?  
  > **username_0**: When I am trying I am geting this error  `[08-Jul-2017 13:42:49 Asia/Kolkata] PHP Fatal error:  Class name must be a valid object or a string in /opt/lampp/htdocs/drupal/sites/all/modules/civicrm/Civi/API/SelectQuery.php on line 101`  
  > **username_1**: Ah, so you haven't created a new entity, this is just some API that returns something based on existing tables? Your approach in the code seems fine then, if you want to add a where clause you'll have to do it all manually though, reading from the params and inserting the where clause in the query you send to `executeQuery`  
  > **username_0**: But I have a table named `civicrm_membershipperiod` already  
  > **username_1**: Then why does your code not select from that table? The error you're getting seems to point to a lack of a BAO. Have you also used civix to generate the entity files?  
  > **username_0**: Yes, I have used civix for creating this entity and followed the steps from this link https://docs.civicrm.org/dev/en/master/extensions/civix/. After creating entity I have copied only DAO file from the development version. I have added BAO file in my question  
  > **username_0**: I found the issue; It my mistake; Created extension as `MembershipPeriod` and table as `civicrm_membershipperiod`. Now by creating extension as `Membershipperiod` solved the problem  
  > **username_1**: I wasn't so much help, but great to hear you got it working  
  
  ---
  
  