Fix another cache usage to use metadata cache
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 18 Aug 2022 06:29:31 +0000 (18:29 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 18 Aug 2022 06:29:31 +0000 (18:29 +1200)
CRM/Core/BAO/CustomField.php
tests/phpunit/api/v3/CustomValueContactTypeTest.php

index c3f8f4a69ba20ba9201bc375a871ee0546136c19..9c1fa009a9c418eeacd88b940a3c264246824fa0 100644 (file)
@@ -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;
   }
 
index 238104895f6e46fed0db9dd52a51d1311923186c..f6f4f32ac6b1e0bd5fce92cd09f813995bd0972a 100644 (file)
@@ -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 {