Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-10421

API not returning requested number of results

    Details

    • Type: Bug
    • Status: Done/Fixed
    • Priority: Minor
    • Resolution: Cannot Reproduce
    • Affects Version/s: 4.1.3
    • Fix Version/s: None
    • Component/s: CiviCRM API
    • Labels:

      Description

      Rest API isn't returning the requested number of records. For example, if we request 50 contacts, (offset: 0, rowCount: 50), it returns 47, if we request 100, it returns 96, and so.

      The SQL query being used to generate the results is returning some duplicate records, which are then being stripped before being sent back to the client, which accounts for the missing records in the API response.

      To solve this we just edited the file:

      civicrm/CRM/Contact/BAO/Query.php

      And added the following line of code before line 3635:

      $sql = str_replace("LIMIT", "GROUP BY contact_a.id LIMIT", $sql);

      so that the method "apiQuery" now looks like this:

      static function apiQuery($params = NULL,
      $returnProperties = NULL,
      $fields = NULL,
      $sort = NULL,
      $offset = 0,
      $row_count = 25,
      $smartGroupCache = TRUE
      ) {
      $query = new CRM_Contact_BAO_Query($params, $returnProperties,
      NULL, TRUE, FALSE, 1,
      FALSE, TRUE, $smartGroupCache
      );

      list($select, $from, $where, $having) = $query->query();
      $options = $query->_options;
      $sql = "$select $from $where $having";
      // add group by
      if ($query->_useGroupBy)

      { $sql .= ' GROUP BY contact_a.id'; }

      if (!empty($sort))

      { $sql .= " ORDER BY $sort "; }

      if ($row_count > 0 && $offset >= 0)

      { $sql .= " LIMIT $offset, $row_count "; }

      $sql = str_replace("LIMIT", "GROUP BY contact_a.id LIMIT", $sql);

      $dao = CRM_Core_DAO::executeQuery($sql);

      $values = array();
      while ($dao->fetch())

      { $values[$dao->contact_id] = $query->store($dao); }

      $dao->free();
      return array($values, $options);
      }

        Attachments

          Activity

            People

            • Assignee:
              lobo Donald A. Lobo
              Reporter:
              simbro Simon Brown
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: