Merge pull request #17719 from civicrm/5.27
[civicrm-core.git] / Civi / Api4 / Generic / DAOGetAction.php
index d922781ada2e049574a2d065aec703437733213e..2b73f18a8ffb3d33a172cd6cc9f4dc8d516baf0a 100644 (file)
@@ -71,20 +71,31 @@ class DAOGetAction extends AbstractGetAction {
   public function _run(Result $result) {
     $this->setDefaultWhereClause();
     $this->expandSelectClauseWildcards();
-    $result->exchangeArray($this->getObjects());
+    $this->getObjects($result);
   }
 
   /**
-   * @return array|int
+   * @param \Civi\Api4\Generic\Result $result
    */
-  protected function getObjects() {
-    $query = new Api4SelectQuery($this);
-
-    $result = $query->run();
-    if (is_array($result)) {
-      \CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($result);
+  protected function getObjects(Result $result) {
+    $getCount = in_array('row_count', $this->getSelect());
+    $onlyCount = $this->getSelect() === ['row_count'];
+
+    if (!$onlyCount) {
+      $query = new Api4SelectQuery($this);
+      $rows = $query->run();
+      \CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($rows);
+      $result->exchangeArray($rows);
+      // No need to fetch count if we got a result set below the limit
+      if (!$this->getLimit() || count($rows) < $this->getLimit()) {
+        $result->rowCount = count($rows) + $this->getOffset();
+        $getCount = FALSE;
+      }
+    }
+    if ($getCount) {
+      $query = new Api4SelectQuery($this);
+      $result->rowCount = $query->getCount();
     }
-    return $result;
   }
 
   /**