EntityRef fields don't get returned and can't be joined in API4
authorJon Goldberg <jon@megaphonetech.com>
Fri, 24 Mar 2023 20:22:38 +0000 (16:22 -0400)
committerColeman Watts <coleman@civicrm.org>
Sun, 26 Mar 2023 20:41:16 +0000 (16:41 -0400)
Civi/Api4/Service/Schema/SchemaMapBuilder.php

index 7401539fe25e34a4e37aa593b5b67033feecbb05..539b56b90927f873e03c0d75e459262fadd1144f 100644 (file)
@@ -107,7 +107,7 @@ class SchemaMapBuilder extends AutoService {
     }
     $fieldData = \CRM_Utils_SQL_Select::from('civicrm_custom_field f')
       ->join('custom_group', 'INNER JOIN civicrm_custom_group g ON g.id = f.custom_group_id')
-      ->select(['g.name as custom_group_name', 'g.table_name', 'g.is_multiple', 'f.name', 'f.data_type', 'label', 'column_name', 'option_group_id', 'serialize'])
+      ->select(['g.name as custom_group_name', 'g.table_name', 'g.is_multiple', 'f.name', 'f.data_type', 'label', 'column_name', 'option_group_id', 'serialize', 'fk_entity'])
       ->where('g.extends IN (@entity)', ['@entity' => $customInfo['extends']])
       ->where('g.is_active')
       ->where('f.is_active')
@@ -136,6 +136,12 @@ class SchemaMapBuilder extends AutoService {
         $customTable->addTableLink('entity_id', $joinable);
       }
 
+      if ($fieldData->data_type === 'EntityReference') {
+        $targetTable = \CRM_Core_BAO_CustomGroup::getTableNameByEntityName($fieldData->fk_entity);
+        $joinable = new Joinable($targetTable, 'id', $fieldData->name);
+        $customTable->addTableLink($fieldData->column_name, $joinable);
+      }
+
       if ($fieldData->data_type === 'ContactReference') {
         $joinable = new Joinable('civicrm_contact', 'id', $fieldData->name);
         if ($fieldData->serialize) {