From 683e39ebaaf30a49a5b5ea512d575552dc68b159 Mon Sep 17 00:00:00 2001 From: Johan Vervloet Date: Thu, 5 Mar 2015 13:12:36 +0100 Subject: [PATCH] Changed key and values of the $select_fields array. Because that seems to be more intuitive. --- api/v3/utils.php | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index c326aeaeae..72d0e60712 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -480,20 +480,12 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { $entity_field_names = _civicrm_api3_field_names( _civicrm_api3_build_fields_array($dao)); - // $select_fields will contain information about the colum names we need to - // select. E.g. - // array( - // "title" => "a.title", - // "custom_7" => "civicrm_value_event_custom_things_4.some_id_7", - // ) - // The keys in the array indicate the names of the fields in the result - // values, the array values are the name of the fields in the query, - // prefixed by the source table. This source table is either a, - // referring to the entity we are querying, either the name of a - // custom field table we need to join. + // $select_fields maps column names to the field names of the result + // values. $select_fields = array(); // always select id. - $select_fields["id"] = "a.id"; + // 'a' is an alias for the entity table we are selecting from. + $select_fields["a.id"] = "id"; // array with elements {'column' => 'value'} // again, column is prefixed by 'a.' or the name of the custom field @@ -504,23 +496,21 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { // Tables we need to join with to retrieve the custom values. $tables_to_join=array(); - // populate $select_fields - // TODO: select some default fields if the user didn't provide a + // TODO: select some (all?) fields if the user didn't provide a // 'return' option. - if (!empty($options['return']) && is_array($options['return'])) { foreach ($options['return'] as $field_name => $value) { if (in_array($field_name, $entity_field_names)) { // select entity field - $select_fields[$field_name] = "a.$field_name"; + $select_fields["a.$field_name"] = $field_name; } else { $cf_id = CRM_Core_BAO_CustomField::getKeyID($field_name); if ($cf_id) { $table_name = $custom_fields[$cf_id]["table_name"]; $column_name = $custom_fields[$cf_id]["column_name"]; - $select_fields["custom_$cf_id"] = "$table_name.$column_name"; + $select_fields["$table_name.$column_name"] = "custom_$cf_id"; if (!in_array($table_name, $tables_to_join)) { $tables_to_join[] = $table_name; } @@ -554,8 +544,8 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { $where = "WHERE 1=1"; $query_params = array(); - foreach ($select_fields as $name => $column) { - $select .= ", $column as $name"; + foreach ($select_fields as $column => $alias) { + $select .= ", $column as $alias"; } foreach ($tables_to_join as $table_name) { @@ -579,9 +569,9 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { $result_dao = CRM_Core_DAO::executeQuery($query, $query_params); while ($result_dao->fetch()) { $result_entities[$result_dao->id] = array(); - foreach ($select_fields as $name => $column) { - if (array_key_exists($name, $result_dao)) { - $result_entities[$result_dao->id][$name] = $result_dao->$name; + foreach ($select_fields as $column => $alias) { + if (array_key_exists($alias, $result_dao)) { + $result_entities[$result_dao->id][$alias] = $result_dao->$alias; } }; } -- 2.25.1