From: Johan Vervloet Date: Thu, 5 Mar 2015 13:10:10 +0000 (+0100) Subject: Populate $select_fields more efficiently. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=5d5c3c21e068ed6f728faadec1d67f4f908d7bcc;p=civicrm-core.git Populate $select_fields more efficiently. --- diff --git a/api/v3/utils.php b/api/v3/utils.php index 2291e105a2..38c88431a0 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -494,42 +494,32 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params) { $tables_to_join=array(); // populate $select_fields - if (empty($options['return']) || !is_array($options['return'])) { - // return every field if no return option exists. - foreach ($entity_field_names as $field_name) { + $return_all_fields = (empty($options['return']) || !is_array($options['return'])); + + // default fields + foreach ($entity_field_names as $field_name) { + if ($return_all_fields || !empty($options['return'][$field_name])) { // 'a.' is an alias for the entity table. $select_fields["a.$field_name"] = $field_name; } - foreach ($custom_fields as $cf_id => $custom_field) { + } + + // custom fields + foreach ($custom_fields as $cf_id => $custom_field) { + $field_name = "custom_$cf_id"; + if ($return_all_fields || !empty($options['return'][$field_name])) { $table_name = $custom_field["table_name"]; $column_name = $custom_field["column_name"]; $select_fields["$table_name.$column_name"] = "custom_$cf_id"; + // remember that we will need to join the correct table. if (!in_array($table_name, $tables_to_join)) { $tables_to_join[] = $table_name; } } } - else { - // look at return option. - foreach ($options['return'] as $field_name => $value) { - if (in_array($field_name, $entity_field_names)) { - // select entity field - $select_fields["a.$field_name"] = $field_name; - } - else { - // always select ID. - $select_fields["a.id"] = "id"; - $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["$table_name.$column_name"] = "custom_$cf_id"; - if (!in_array($table_name, $tables_to_join)) { - $tables_to_join[] = $table_name; - } - } - } - } + if (!in_array("a.id", $select_fields)) { + // Always select the ID. + $select_fields["a.id"] = "id"; } // populate $where_clauses