Merge pull request #1544 from eileenmcnaughton/master
authorKurund Jalmi <kurund@civicrm.org>
Fri, 30 Aug 2013 04:37:11 +0000 (21:37 -0700)
committerKurund Jalmi <kurund@civicrm.org>
Fri, 30 Aug 2013 04:37:11 +0000 (21:37 -0700)
CRM-9493 - fix for group contact cache hanging.

CRM/Contact/BAO/GroupContactCache.php
api/api.php

index 90b2cdd62ff403c2cd77cc0f6b5833f5b6df0d7e..14a4c7c70d98d7ac318f8130e9c5d778a4536168 100644 (file)
@@ -209,14 +209,19 @@ AND    g.refresh_date IS NULL
     // sort the values so we put group IDs in front and hence optimize
     // mysql storage (or so we think) CRM-9493
     sort($values);
-
+    $tempTable = 'civicrm_temp_group_contact_cache' . rand(0,2000);
     // to avoid long strings, lets do BULK_INSERT_COUNT values at a time
     while (!empty($values)) {
       $processed = TRUE;
       $input     = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
       $str       = implode(',', $input);
-      $sql       = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;";
+      $sql       = "CREATE TEMPORARY TABLE $tempTable  $str;";
       CRM_Core_DAO::executeQuery($sql);
+      CRM_Core_DAO::executeQuery(
+      "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
+      SELECT DISTINCT id, group_id FROM $tempTable
+      ");
+      CRM_Core_DAO::executeQuery(" DROP TABLE $tempTable");
     }
     self::updateCacheTime($groupID, $processed);
   }
@@ -480,16 +485,22 @@ WHERE  civicrm_group_contact.status = 'Added'
 
     $groupIDs = array($groupID);
     self::remove($groupIDs);
-
     $processed = FALSE;
+    $tempTable = 'civicrm_temp_group_contact_cache' . rand(0,2000);
     foreach (array($sql, $sqlB) as $selectSql) {
       if (!$selectSql) {
         continue;
       }
-      $insertSql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) ($selectSql);";
-      $processed = TRUE;
+      $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);";
+      $processed = TRUE; // FIXME
       $result = CRM_Core_DAO::executeQuery($insertSql);
+      CRM_Core_DAO::executeQuery(
+        "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
+        SELECT DISTINCT id, group_id FROM $tempTable
+      ");
+      CRM_Core_DAO::executeQuery(" DROP TABLE $tempTable");
     }
+
     self::updateCacheTime($groupIDs, $processed);
 
     if ($group->children) {
index f42d5ac8da50991408164b80109849b1f177ad77..918e13a10fc8c310b869fdc285fef0194610b2f6 100644 (file)
@@ -43,7 +43,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
     $errorScope = CRM_Core_TemporaryErrorScope::useException();
     // look up function, file, is_generic
     $apiRequest += _civicrm_api_resolve($apiRequest);
-    if (strtolower($action) == 'create' || strtolower($action) == 'delete') {
+    if (strtolower($action) == 'create' || strtolower($action) == 'delete' || strtolower($action) == 'submit') {
       $apiRequest['is_transactional'] = 1;
       $transaction = new CRM_Core_Transaction();
     }