$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