From 9811efd493b80a25f02533936603a4e69e6cbd9c Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Tue, 16 Jun 2020 13:43:49 -0400 Subject: [PATCH] handle less common dynamic reference cases --- CRM/Core/DAO.php | 2 +- CRM/Dedupe/MergeHandler.php | 6 ++++-- CRM/Dedupe/Merger.php | 7 +++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 871a3c1242..1e245457ec 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -2453,7 +2453,7 @@ SELECT contact_id $coreReferences = CRM_Core_DAO::getReferencesToTable($tableName); foreach ($coreReferences as $coreReference) { if ($coreReference instanceof \CRM_Core_Reference_Dynamic) { - \Civi::$statics[__CLASS__]['contact_references_dynamic'][$tableName][$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey(); + \Civi::$statics[__CLASS__]['contact_references_dynamic'][$tableName][$coreReference->getReferenceTable()][] = [$coreReference->getReferenceKey(), $coreReference->getTypeColumn()]; } } } diff --git a/CRM/Dedupe/MergeHandler.php b/CRM/Dedupe/MergeHandler.php index db454c0e70..e48352cfd7 100644 --- a/CRM/Dedupe/MergeHandler.php +++ b/CRM/Dedupe/MergeHandler.php @@ -123,7 +123,7 @@ class CRM_Dedupe_MergeHandler { public function getTablesDynamicallyRelatedToContactTable() { if (!isset(\Civi::$statics[__CLASS__]['dynamic'])) { \Civi::$statics[__CLASS__]['dynamic'] = []; - foreach (CRM_Core_DAO::getDynamicReferencesToTable('civicrm_contact') as $tableName => $field) { + foreach (CRM_Core_DAO::getDynamicReferencesToTable('civicrm_contact') as $tableName => $fields) { if ($tableName === 'civicrm_financial_item') { // It turns out that civicrm_financial_item does not have an index on entity_table (only as the latter // part of a entity_id/entity_table index which probably is not adding any value over & above entity_id @@ -131,7 +131,9 @@ class CRM_Dedupe_MergeHandler { // values for entity_table in the schema. continue; } - $sql[] = "(SELECT '$tableName' as civicrm_table, '{$field[0]}' as field_name FROM $tableName WHERE entity_table = 'civicrm_contact' LIMIT 1)"; + foreach ($fields as $field) { + $sql[] = "(SELECT '$tableName' as civicrm_table, '{$field[0]}' as field_name FROM $tableName WHERE {$field[1]} = 'civicrm_contact' LIMIT 1)"; + } } $sqlString = implode(' UNION ', $sql); if ($sqlString) { diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 031568ea4a..9f161af164 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -492,6 +492,7 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m $otherId = $mergeHandler->getToRemoveID(); $cidRefs = self::cidRefs(); $eidRefs = $mergeHandler->getTablesDynamicallyRelatedToContactTable(); + $dynamicRefs = CRM_Core_DAO::getDynamicReferencesToTable('civicrm_contact'); $cpTables = self::cpTables(); $paymentTables = self::paymentTables(); self::filterRowBasedCustomDataFromCustomTables($cidRefs); @@ -548,8 +549,10 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m } if (isset($eidRefs[$table])) { - $sqls[] = "UPDATE IGNORE $table SET {$eidRefs[$table]}= $mainId WHERE {$eidRefs[$table]} = $otherId AND entity_table = 'civicrm_contact'"; - $sqls[] = "DELETE FROM $table WHERE {$eidRefs[$table]} = $otherId AND entity_table = 'civicrm_contact'"; + foreach ($dynamicRefs[$table] as $dynamicRef) { + $sqls[] = "UPDATE IGNORE $table SET {$dynamicRef[0]}= $mainId WHERE {$dynamicRef[0]} = $otherId AND {$dynamicRef[1]} = 'civicrm_contact'"; + $sqls[] = "DELETE FROM $table WHERE {$dynamicRef[0]} = $otherId AND {$dynamicRef[1]} = 'civicrm_contact'"; + } } } -- 2.25.1