From: Coleman Watts Date: Fri, 1 Jan 2016 03:45:47 +0000 (-0500) Subject: Fix api basic get return properties X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=357f9d0e767cd9b1d03f50fffd943e828e01a876;p=civicrm-core.git Fix api basic get return properties Api basic get was crashing when a non-existant field was in the return properties. But there are legitimate reasons for this e.g. apis with the ability to return extra info when requested via magic return params. Also fixes handling of field aliases in the return properties. --- diff --git a/api/v3/utils.php b/api/v3/utils.php index 66643e8581..3281beada0 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -494,8 +494,13 @@ function _civicrm_api3_get_using_utils_sql($dao_name, $params, $isFillUniqueFiel $getFieldsResult = civicrm_api3($entity, 'getfields', array('action' => 'get')); $getFieldsResult = $getFieldsResult['values']; foreach ($getFieldsResult as $getFieldKey => $getFieldSpec) { - $uniqueAliases[$getFieldKey] = $getFieldSpec['name']; - $uniqueAliases[$getFieldSpec['name']] = $getFieldSpec['name']; + if (in_array($getFieldSpec['name'], $entity_field_names)) { + $uniqueAliases[$getFieldKey] = $getFieldSpec['name']; + $uniqueAliases[$getFieldSpec['name']] = $getFieldSpec['name']; + foreach (CRM_Utils_Array::value('api.aliases', $getFieldSpec, array()) as $alias) { + $uniqueAliases[$alias] = $getFieldSpec['name']; + } + } } // $select_fields maps column names to the field names of the result @@ -516,8 +521,8 @@ function _civicrm_api3_get_using_utils_sql($dao_name, $params, $isFillUniqueFiel $return = $return_all_fields ? array_fill_keys($entity_field_names, 1) : $options['return']; // default fields - foreach (array_keys($return) as $field_name) { - if (!empty($uniqueAliases[$field_name]) && (CRM_Core_BAO_CustomField::getKeyID($field_name) == FALSE)) { + foreach ($return as $field_name => $include) { + if ($include && !empty($uniqueAliases[$field_name])) { // 'a.' is an alias for the entity table. $select_fields["a.{$uniqueAliases[$field_name]}"] = $uniqueAliases[$field_name]; } @@ -1632,14 +1637,12 @@ function _civicrm_api3_check_required_fields($params, $daoName, $return = FALSE) * * @return array */ -function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $entity = "", $sql = NULL, $uniqueFields = FALSE) { - +function _civicrm_api3_basic_get($bao_name, $params, $returnAsSuccess = TRUE, $entity = "", $sql = NULL, $uniqueFields = FALSE) { + $result = _civicrm_api3_get_using_utils_sql($bao_name, $params, $uniqueFields, $sql); if ($returnAsSuccess) { - return civicrm_api3_create_success(_civicrm_api3_get_using_utils_sql($bao_name, $params, $uniqueFields, $sql), $params, $entity, 'get'); - } - else { - return _civicrm_api3_get_using_utils_sql($bao_name, $params, $uniqueFields, $sql); + return civicrm_api3_create_success($result, $params, $entity, 'get'); } + return $result; } /**