Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Avoid unnecessary operations on the database. #31558

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions CRM/Mailing/BAO/MailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,8 @@ public function writeToDB(
) {
static $activityTypeID = NULL;
static $writeActivity = NULL;
static $activityID = NULL;
static $targetRecordID = NULL;

if (!empty($deliveredParams)) {
CRM_Mailing_Event_BAO_MailingEventDelivered::bulkCreate($deliveredParams);
Expand Down Expand Up @@ -705,20 +707,23 @@ public function writeToDB(

//check whether activity is already created for this mailing.
//if yes then create only target contact record.
$query = "
if (!$activityID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Picking up @ufundo's feedback my thinking would be to do something like here

if (!isset(Civi::$statics[CLASS]['activity_ids'])) {
Civi::$statics[CLASS]['activity_ids'] = [];
}
if (!isset(Civi::$statics[CLASS]['activity_ids'][$this->mailing_id])) {
// run the query
and set Civi::$statics[CLASS]['activity_ids'][$this->mailing_id] = found activity id and then just use Civi::$statics[CLASS]['activity_ids'][$this->mailing_id] as the reference to the activity id going forward or something

$query = "
SELECT id
FROM civicrm_activity
WHERE civicrm_activity.activity_type_id = %1
AND civicrm_activity.source_record_id = %2
";

$queryParams = [
1 => [$activityTypeID, 'Integer'],
2 => [$this->mailing_id, 'Integer'],
];
$activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams);
$targetRecordID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets');

$queryParams = [
1 => [$activityTypeID, 'Integer'],
2 => [$this->mailing_id, 'Integer'],
];
$activityID = CRM_Core_DAO::singleValueQuery($query, $queryParams);
}
if (!$targetRecordID) {
$targetRecordID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets');
}
$activityTargets = [];
foreach ($targetParams as $id) {
$activityTargets[$id] = ['contact_id' => (int) $id];
Expand Down Expand Up @@ -746,7 +751,9 @@ public function writeToDB(
}

try {
$activity = civicrm_api3('Activity', 'create', $activity);
if (empty($activity['id'])) {
$activity = civicrm_api3('Activity', 'create', $activity);
}
ActivityContact::save(FALSE)->setRecords($activityTargets)->setDefaults(['activity_id' => $activity['id'], 'record_type_id' => $targetRecordID])->execute();
}
catch (Exception $e) {
Expand Down