From 91c00d11d405e665cf9e0fd4260b59b1a142b220 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 11 Dec 2019 22:05:08 +1300 Subject: [PATCH] Remove unnecessary query when updating org, if no name udpate. This removes an unnecessary query that can be locking at high volume. The updateCurrentEmployer function sets the organization_name for any related contacts. We don't need to do this if we are creating the organization for the first time, or updating the organization but not setting (passing in) organization_name. We can check & avoid in php. Also we don't need to check for an existing employer_id unless we are dealing with an Individual record. We can skip that query for Orgs & Households. These are cheap queries but at high volume can contribute to locks --- CRM/Contact/BAO/Contact.php | 12 ++++++------ CRM/Contact/BAO/Contact/Utils.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 51e8334a05..644b365683 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -98,14 +98,14 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { * @param array $params * (reference) an assoc array of name/value pairs. * - * @return CRM_Contact_BAO_Contact|CRM_Core_Error|NULL + * @return CRM_Contact_DAO_Contact|CRM_Core_Error|NULL * Created or updated contact object or error object. * (error objects are being phased out in favour of exceptions) * @throws \Exception */ public static function add(&$params) { $contact = new CRM_Contact_DAO_Contact(); - + $contactID = CRM_Utils_Array::value('contact_id', $params); if (empty($params)) { return NULL; } @@ -147,7 +147,7 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { $allNull = $contact->copyValues($params); - $contact->id = CRM_Utils_Array::value('contact_id', $params); + $contact->id = $contactID; if ($contact->contact_type === 'Individual') { $allNull = FALSE; @@ -186,7 +186,7 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { // Even if we don't need $employerId, it's important to call getFieldValue() before // the contact is saved because we want the existing value to be cached. // createCurrentEmployerRelationship() needs the old value not the updated one. CRM-10788 - $employerId = empty($contact->id) ? NULL : CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id'); + $employerId = (!$contactID || $contact->contact_type !== 'Individual') ? NULL : CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id'); if (!$allNull) { $contact->save(); @@ -215,8 +215,8 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { } } - // Update cached employer name. - if ($contact->contact_type === 'Organization') { + // Update cached employer name if the name of an existing organization is being updated. + if ($contact->contact_type === 'Organization' && !empty($params['organization_name']) && $contactID) { CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id); } diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index b7176c4837..a1c2262135 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -364,7 +364,7 @@ WHERE contact_a.id ={$contactId} AND contact_b.id={$orgId}; "; SET contact_a.organization_name=contact_b.organization_name WHERE contact_a.employer_id=contact_b.id AND contact_b.id={$organizationId}; "; - $dao = CRM_Core_DAO::executeQuery($query); + CRM_Core_DAO::executeQuery($query); } /** -- 2.25.1