From 426bdf863001c417da44a9448bbf52bb9b486aae Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 19 Apr 2020 19:56:26 -0400 Subject: [PATCH] Revert "APIv4 - Add rudimentary support for groupBy" This reverts commit fba513f62ec8815e08fa838e0d0501279bf34501. --- Civi/Api4/Generic/DAOGetAction.php | 32 ------------------------- Civi/Api4/Query/Api4SelectQuery.php | 36 +++++------------------------ ang/api4Explorer/Explorer.html | 29 ++++++----------------- ang/api4Explorer/Explorer.js | 20 +++++++--------- 4 files changed, 21 insertions(+), 96 deletions(-) diff --git a/Civi/Api4/Generic/DAOGetAction.php b/Civi/Api4/Generic/DAOGetAction.php index ae29e3992a..100173d962 100644 --- a/Civi/Api4/Generic/DAOGetAction.php +++ b/Civi/Api4/Generic/DAOGetAction.php @@ -44,13 +44,6 @@ class DAOGetAction extends AbstractGetAction { */ protected $select = []; - /** - * Field(s) by which to group the results. - * - * @var array - */ - protected $groupBy = []; - public function _run(Result $result) { $this->setDefaultWhereClause(); $this->expandSelectClauseWildcards(); @@ -70,29 +63,4 @@ class DAOGetAction extends AbstractGetAction { 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; - } - } diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 4e7082a4dc..de08a7b152 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -60,11 +60,6 @@ class Api4SelectQuery extends SelectQuery { */ public $debugOutput = NULL; - /** - * @var array - */ - public $groupBy = []; - /** * @param \Civi\Api4\Generic\DAOGetAction $apiGet */ @@ -73,7 +68,6 @@ class Api4SelectQuery extends SelectQuery { $this->checkPermissions = $apiGet->getCheckPermissions(); $this->select = $apiGet->getSelect(); $this->where = $apiGet->getWhere(); - $this->groupBy = $apiGet->getGroupBy(); $this->orderBy = $apiGet->getOrderBy(); $this->limit = $apiGet->getLimit(); $this->offset = $apiGet->getOffset(); @@ -106,7 +100,6 @@ class Api4SelectQuery extends SelectQuery { $this->buildWhereClause(); $this->buildOrderBy(); $this->buildLimit(); - $this->buildGroupBy(); return $this->query->toSQL(); } @@ -122,21 +115,20 @@ class Api4SelectQuery extends SelectQuery { $this->debugOutput['sql'][] = $sql; } $query = \CRM_Core_DAO::executeQuery($sql); - $i = 0; + while ($query->fetch()) { - $id = $query->id ?? $i++; if (in_array('row_count', $this->select)) { $results[]['row_count'] = (int) $query->c; break; } - $results[$id] = []; + $results[$query->id] = []; foreach ($this->select as $alias) { $returnName = $alias; if ($this->isOneToOneField($alias)) { $alias = str_replace('.', '_', $alias); - $results[$id][$returnName] = property_exists($query, $alias) ? $query->$alias : NULL; + $results[$query->id][$returnName] = property_exists($query, $alias) ? $query->$alias : NULL; } - } + }; } $event = new PostSelectQueryEvent($results, $this); \Civi::dispatcher()->dispatch(Events::POST_SELECT_QUERY, $event); @@ -153,10 +145,8 @@ class Api4SelectQuery extends SelectQuery { return; } else { - // Always select ID (unless we're doing groupBy). - if (!$this->groupBy) { - $this->select = array_merge(['id'], $this->select); - } + // Always select id field + $this->select = array_merge(['id'], $this->select); // Expand wildcards in joins (the api wrapper already expanded non-joined wildcards) $wildFields = array_filter($this->select, function($item) { @@ -219,20 +209,6 @@ class Api4SelectQuery extends SelectQuery { } } - /** - * - */ - protected function buildGroupBy() { - foreach ($this->groupBy as $field) { - if ($this->isOneToOneField($field) && $this->getField($field)) { - $this->query->groupBy($field['sql_name']); - } - else { - throw new \API_Exception("Invalid field. Cannot group by $field"); - } - } - } - /** * Recursively validate and transform a branch or leaf clause array to SQL. * diff --git a/ang/api4Explorer/Explorer.html b/ang/api4Explorer/Explorer.html index ad35e38103..659917ecb2 100644 --- a/ang/api4Explorer/Explorer.html +++ b/ang/api4Explorer/Explorer.html @@ -84,35 +84,20 @@
-
- groupBy * -
-
- - -
-
-
- -
-
+
orderBy * -
-
- - - -
+
+ +
-
chain
diff --git a/ang/api4Explorer/Explorer.js b/ang/api4Explorer/Explorer.js index f75443dcf4..09db7d1d02 100644 --- a/ang/api4Explorer/Explorer.js +++ b/ang/api4Explorer/Explorer.js @@ -237,7 +237,7 @@ }; $scope.isSpecial = function(name) { - var specialParams = ['select', 'fields', 'action', 'where', 'values', 'defaults', 'orderBy', 'chain', 'groupBy']; + var specialParams = ['select', 'fields', 'action', 'where', 'values', 'defaults', 'orderBy', 'chain']; return _.contains(specialParams, name); }; @@ -374,7 +374,7 @@ deep: format === 'json' }); } - if (typeof objectParams[name] !== 'undefined' || name === 'groupBy') { + if (typeof objectParams[name] !== 'undefined') { $scope.$watch('params.' + name, function(values) { // Remove empty values _.each(values, function(clause, index) { @@ -387,17 +387,13 @@ var field = value; $timeout(function() { if (field) { - if (name === 'groupBy') { - $scope.params[name].push(field); - } else { - var defaultOp = _.cloneDeep(objectParams[name]); - if (name === 'chain') { - var num = $scope.params.chain.length; - defaultOp[0] = field; - field = 'name_me_' + num; - } - $scope.params[name].push([field, defaultOp]); + var defaultOp = _.cloneDeep(objectParams[name]); + if (name === 'chain') { + var num = $scope.params.chain.length; + defaultOp[0] = field; + field = 'name_me_' + num; } + $scope.params[name].push([field, defaultOp]); $scope.controls[name] = null; } }); -- 2.25.1