Populate $select_fields more efficiently.
authorJohan Vervloet <johanv@johanv.org>
Thu, 5 Mar 2015 13:10:10 +0000 (14:10 +0100)
committereileenmcnaugton <eileen@fuzion.co.nz>
Wed, 12 Aug 2015 23:39:26 +0000 (11:39 +1200)
api/v3/utils.php

index 2291e105a284456120c1595aa48a0f0681e81537..38c88431a0402abdb09871db5e0f4251c47b2f9e 100644 (file)
@@ -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