Merge pull request #17150 from civicrm/5.25
[civicrm-core.git] / Civi / Api4 / Generic / DAOGetAction.php
index ae29e3992a3f116bbddb20844e3e1d23cdb6fe4f..ab7d85b966d3ab1802b2b0dd2219a95f54b34854 100644 (file)
@@ -29,6 +29,9 @@ use Civi\Api4\Query\Api4SelectQuery;
  * Use the `select` param to determine which fields are returned, defaults to `[*]`.
  *
  * Perform joins on other related entities using a dot notation.
+ *
+ * @method $this setHaving(array $clauses)
+ * @method array getHaving()
  */
 class DAOGetAction extends AbstractGetAction {
   use Traits\DAOActionTrait;
@@ -51,6 +54,15 @@ class DAOGetAction extends AbstractGetAction {
    */
   protected $groupBy = [];
 
+  /**
+   * Clause for filtering results after grouping and filters are applied.
+   *
+   * Each expression should correspond to an item from the SELECT array.
+   *
+   * @var array
+   */
+  protected $having = [];
+
   public function _run(Result $result) {
     $this->setDefaultWhereClause();
     $this->expandSelectClauseWildcards();
@@ -95,4 +107,19 @@ class DAOGetAction extends AbstractGetAction {
     return $this;
   }
 
+  /**
+   * @param string $expr
+   * @param string $op
+   * @param mixed $value
+   * @return $this
+   * @throws \API_Exception
+   */
+  public function addHaving(string $expr, string $op, $value = NULL) {
+    if (!in_array($op, \CRM_Core_DAO::acceptedSQLOperators())) {
+      throw new \API_Exception('Unsupported operator');
+    }
+    $this->having[] = [$expr, $op, $value];
+    return $this;
+  }
+
 }