APIv4 - Prevent errors during upgrade when loading custom field metadata
authorColeman Watts <coleman@civicrm.org>
Sun, 26 Mar 2023 23:35:29 +0000 (19:35 -0400)
committerColeman Watts <coleman@civicrm.org>
Sun, 26 Mar 2023 23:35:29 +0000 (19:35 -0400)
Civi/Api4/Service/Schema/SchemaMapBuilder.php

index 539b56b90927f873e03c0d75e459262fadd1144f..16de8c026de86814b629baa3cfe9c61b2b188465 100644 (file)
@@ -105,9 +105,15 @@ class SchemaMapBuilder extends AutoService {
     if (!$customInfo) {
       return;
     }
+    $select = ['f.name', 'f.data_type', 'f.label', 'f.column_name', 'f.option_group_id', 'f.serialize', 'f.fk_entity'];
+    // Prevent errors during upgrade by only selecting fields supported by the current version
+    $supportedFields = \CRM_Utils_Array::prefixKeys(\CRM_Core_BAO_CustomField::getSupportedFields(), 'f.');
+    $select = array_intersect($select, array_keys($supportedFields));
+    // Also select fields from the custom_group table (these fields are so old we don't have to worry about upgrade issues)
+    $select = array_merge(['g.name as custom_group_name', 'g.table_name', 'g.is_multiple'], $select);
     $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', 'fk_entity'])
+      ->select($select)
       ->where('g.extends IN (@entity)', ['@entity' => $customInfo['extends']])
       ->where('g.is_active')
       ->where('f.is_active')
@@ -136,8 +142,8 @@ class SchemaMapBuilder extends AutoService {
         $customTable->addTableLink('entity_id', $joinable);
       }
 
-      if ($fieldData->data_type === 'EntityReference') {
-        $targetTable = \CRM_Core_BAO_CustomGroup::getTableNameByEntityName($fieldData->fk_entity);
+      if ($fieldData->data_type === 'EntityReference' && isset($fieldData->fk_entity)) {
+        $targetTable = AllCoreTables::getTableForEntityName($fieldData->fk_entity);
         $joinable = new Joinable($targetTable, 'id', $fieldData->name);
         $customTable->addTableLink($fieldData->column_name, $joinable);
       }