From: Coleman Watts Date: Thu, 5 Nov 2020 22:36:52 +0000 (-0500) Subject: CustomField - simplify tableName lookup during bulkSave and fix bug X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=03e3a32ecdafcf826624b92af55ade2e66d13e89;p=civicrm-core.git CustomField - simplify tableName lookup during bulkSave and fix bug This cleans up the tableName lookup to use the method used throughout this class. The extra optimization (local caching in the $tables variable) was actually less efficient because CRM_Core_DAO::getFieldValue caches its results, and is used elsewhere so the value is likely to already be cached. The bug is that the same table name was repeatedly being passed into $logging->fixSchemaDifferencesFor(). --- diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 66a5cf26a2..4a0ae0fba3 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -114,7 +114,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { * @throws \CiviCRM_API3_Exception */ public static function bulkSave($bulkParams, $defaults = []) { - $addedColumns = $sql = $tables = $customFields = []; + $addedColumns = $sql = $customFields = []; foreach ($bulkParams as $index => $fieldParams) { $params = array_merge($defaults, $fieldParams); $customField = self::createCustomFieldRecord($params); @@ -122,17 +122,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { if (!isset($params['custom_group_id'])) { $params['custom_group_id'] = civicrm_api3('CustomField', 'getvalue', ['id' => $customField->id, 'return' => 'custom_group_id']); } - if (!isset($params['table_name'])) { - if (!isset($tables[$params['custom_group_id']])) { - $tables[$params['custom_group_id']] = civicrm_api3('CustomGroup', 'getvalue', [ - 'id' => $params['custom_group_id'], - 'return' => 'table_name', - ]); - } - $params['table_name'] = $tables[$params['custom_group_id']]; - } - $sql[$params['table_name']][] = $fieldSQL; - $addedColumns[$params['table_name']][] = $customField->name; + $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customField->custom_group_id, 'table_name'); + $sql[$tableName][] = $fieldSQL; + $addedColumns[$tableName][] = $customField->name; $customFields[$index] = $customField; } @@ -145,7 +137,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]); } - Civi::service('sql_triggers')->rebuild($params['table_name'], TRUE); + Civi::service('sql_triggers')->rebuild($tableName, TRUE); } CRM_Utils_System::flushCache(); foreach ($customFields as $index => $customField) {