From 6470af0f4d7391ae837f7f750475275421e3a1eb Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 18 Aug 2022 18:29:31 +1200 Subject: [PATCH] Fix another cache usage to use metadata cache --- CRM/Core/BAO/CustomField.php | 35 ++++++++++--------- .../api/v3/CustomValueContactTypeTest.php | 4 +-- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index c3f8f4a69b..9c1fa009a9 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -2244,33 +2244,34 @@ WHERE id IN ( %1, %2 ) * * @param int $fieldID * The fieldID of the custom field. - * @param bool $force - * Force the sql to be run again (primarily used for tests). * * @return array * fatal is fieldID does not exists, else array of tableName, columnName * @throws \CRM_Core_Exception */ - public static function getTableColumnGroup($fieldID, $force = FALSE) { - $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}"; - $cache = CRM_Utils_Cache::singleton(); - $fieldValues = $cache->get($cacheKey); - if (empty($fieldValues) || $force) { - $query = " + public static function getTableColumnGroup($fieldID): array { + global $tsLocale; + // check if we can get the field values from the system cache + $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}_$tsLocale"; + if (Civi::cache('metadata')->has($cacheKey)) { + return Civi::cache('metadata')->get($cacheKey); + } + + $query = ' SELECT cg.table_name, cf.column_name, cg.id FROM civicrm_custom_group cg, - civicrm_custom_field cf + civicrm_custom_field cf WHERE cf.custom_group_id = cg.id -AND cf.id = %1"; - $params = [1 => [$fieldID, 'Integer']]; - $dao = CRM_Core_DAO::executeQuery($query, $params); +AND cf.id = %1'; + $params = [1 => [$fieldID, 'Integer']]; + $dao = CRM_Core_DAO::executeQuery($query, $params); - if (!$dao->fetch()) { - throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID); - } - $fieldValues = [$dao->table_name, $dao->column_name, $dao->id]; - $cache->set($cacheKey, $fieldValues); + if (!$dao->fetch()) { + throw new CRM_Core_Exception('Cannot find table and column information for Custom Field ' . $fieldID); } + $fieldValues = [$dao->table_name, $dao->column_name, $dao->id]; + Civi::cache('metadata')->set($cacheKey, $fieldValues); + return $fieldValues; } diff --git a/tests/phpunit/api/v3/CustomValueContactTypeTest.php b/tests/phpunit/api/v3/CustomValueContactTypeTest.php index 238104895f..f6f4f32ac6 100644 --- a/tests/phpunit/api/v3/CustomValueContactTypeTest.php +++ b/tests/phpunit/api/v3/CustomValueContactTypeTest.php @@ -86,8 +86,8 @@ class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase { $this->organizationSponsor = $this->organizationCreate($params); //refresh php cached variables CRM_Core_PseudoConstant::flush(); - CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndividualField['id'], TRUE); - CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndiStudentField['id'], TRUE); + CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndividualField['id']); + CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndiStudentField['id']); } public function tearDown(): void { -- 2.25.1