diff --git a/Civi/ContactCats/Processor.php b/Civi/ContactCats/Processor.php index a21d17c..f8e977c 100644 --- a/Civi/ContactCats/Processor.php +++ b/Civi/ContactCats/Processor.php @@ -188,20 +188,37 @@ class Processor { } } + /** + * Ensure we have one category row per not-trashed contact and zero the next_category field. + */ protected function resetTable() { Civi::log()->debug('Resetting table', ['=' => 'start,timed']); - // clear out temp space. + + // Delete our data for deleted contacts. + $dao = CRM_Core_DAO::executeQuery(<<N) { + Civi::log()->debug("Deleted category data for {$dao->N} trashed contacts"); + } + $dao->free(); + + // Zero our internal next_category field. CRM_Core_DAO::executeQuery("UPDATE civicrm_contact_category SET next_category = 0;")->free(); - Civi::log()->debug('Resetting table stage 2'); + Civi::log()->debug('stage 2'); + // ensure we have all our contacts covered. // Q: is it quicker to do a WHERE NOT EXISTS? A: nope. CRM_Core_DAO::executeQuery(<<free(); - Civi::log()->debug('', ['=' => 'pop']); + Civi::log()->debug('Done resetting table', ['=' => 'pop']); } /** @@ -246,7 +263,8 @@ class Processor { unset($apiParams['orderBy']); $contactIDs = civicrm_api4($search['api_entity'], 'get', $apiParams)->column($contactIdKey); - // Unsure if this batching is needed + // Unsure if this batching is needed. + // It would be better if we could access the select SQL directly and do it all in an SQL update. $batchSize = 10000; while ($batch = array_splice($contactIDs, 0, $batchSize)) { $batch = implode(',', $batch); @@ -261,7 +279,7 @@ class Processor { SQL; CRM_Core_DAO::executeQuery($sql)->free(); } - Civi::log()->debug('', ['=' => 'pop']); + Civi::log()->debug('Done', ['=' => 'pop']); } protected function assignDefaultCategory(array $cat) {