X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=Civi%2FApi4%2FGeneric%2FDAOGetAction.php;h=ab7d85b966d3ab1802b2b0dd2219a95f54b34854;hb=6bcf9fe3712f184a9bcf77df22199addd0a7ab46;hp=da97a68d40146f5aede6a509156834b9fc1abf36;hpb=67b7cfa19f3cd398d3b2538681cb7311a9c6fa79;p=civicrm-core.git diff --git a/Civi/Api4/Generic/DAOGetAction.php b/Civi/Api4/Generic/DAOGetAction.php index da97a68d40..ab7d85b966 100644 --- a/Civi/Api4/Generic/DAOGetAction.php +++ b/Civi/Api4/Generic/DAOGetAction.php @@ -21,12 +21,17 @@ namespace Civi\Api4\Generic; +use Civi\Api4\Query\Api4SelectQuery; + /** - * Retrieve $ENTITYs based on criteria specified in the `where` parameter. + * Retrieve $ENTITIES based on criteria specified in the `where` parameter. * * 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; @@ -42,10 +47,79 @@ class DAOGetAction extends AbstractGetAction { */ protected $select = []; + /** + * Field(s) by which to group the results. + * + * @var array + */ + 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(); $result->exchangeArray($this->getObjects()); } + /** + * @return array|int + */ + protected function getObjects() { + $query = new Api4SelectQuery($this); + + $result = $query->run(); + if (is_array($result)) { + \CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($result); + } + return $result; + } + + /** + * @return array + */ + public function getGroupBy(): array { + return $this->groupBy; + } + + /** + * @param array $groupBy + * @return $this + */ + public function setGroupBy(array $groupBy) { + $this->groupBy = $groupBy; + return $this; + } + + /** + * @param string $field + * @return $this + */ + public function addGroupBy(string $field) { + $this->groupBy[] = $field; + 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; + } + }