Skip to content

Commit

Permalink
Merge pull request #352 from krishgopi/master
Browse files Browse the repository at this point in the history
Prevent duplicate rows db error while collecting Mailchimp data
  • Loading branch information
cmolava authored Jan 25, 2022
2 parents b449101 + 74039df commit 184ab71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CRM/Mailchimp/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public function collectMailchimp($mode) {
//
// Main loop of all the records.
$collected = 0;
// Note hashes being inserted to prevent db duplicate errors.
$hashes = [];
while ($members = $fetch_batch()) {
$start = microtime(TRUE);
foreach ($members as $member) {
Expand Down Expand Up @@ -220,6 +222,13 @@ public function collectMailchimp($mode) {
// email. So we don't count an email mismatch as a problem.
// $hash = md5($member->email_address . $first_name . $last_name . $interests);
$hash = md5($first_name . $last_name . $interests);
// Prevent duplicate rows db error.
if (!empty($hashes[$hash])) {
continue;
} else {
$hashes[$hash] = 1;
}

// run insert prepared statement
$result = $db->execute($insert, [
$member->email_address,
Expand Down
9 changes: 8 additions & 1 deletion CRM/Mailchimp/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ abstract function invoke( $numParams,
* @access public
*/
static function alterCiviDataforMailchimp ($contactID, $email, &$contactData, &$contactCustomData) {
return self::singleton( )->invoke( 4, $contactID, $email, $contactData, $contactCustomData, self::$_nullObject, self::$_nullObject, 'civicrm_alterCiviDataforMailchimp');
$civiVersion = CRM_Core_BAO_Domain::version();
//from CiviCRM 5.0 the invoke function expects array of parameter names as first param
if (version_compare($civiVersion, '5.0', '<')) {
$numParams = 4;
} else {
$numParams = array('contactID', 'email', 'contactData', 'contactCustomData');
}
return self::singleton( )->invoke( $numParams, $contactID, $email, $contactData, $contactCustomData, self::$_nullObject, self::$_nullObject, 'civicrm_alterCiviDataforMailchimp');
}
}

0 comments on commit 184ab71

Please sign in to comment.