Merge pull request #14250 from totten/master-select-null
[civicrm-core.git] / CRM / Core / BAO / UFMatch.php
index dab5fa387930e2ac7850ae58e99d16445010927b..778dd29f42ea9a7911b2d77b4afc9b3e0270ea71 100644 (file)
@@ -60,7 +60,6 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
     return $dao;
   }
 
-
   /**
    * Given a UF user object, make sure there is a contact
    * object for this user. If the user has new values, we need
@@ -324,7 +323,6 @@ AND    domain_id    = %4
 
       if (!$conflict) {
         $ufmatch = CRM_Core_BAO_UFMatch::create((array) $ufmatch);
-        $ufmatch->free();
         $newContact = TRUE;
         $transaction->commit();
       }
@@ -432,25 +430,23 @@ AND    domain_id    = %4
       $contactDetails = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId);
 
       if (trim($contactDetails[1])) {
+        //update if record is found but different
         $emailID = $contactDetails[3];
-        //update if record is found
-        $query = "UPDATE  civicrm_email
-                     SET email = %1
-                     WHERE id =  %2";
-        $p = [
-          1 => [$emailAddress, 'String'],
-          2 => [$emailID, 'Integer'],
-        ];
-        $dao = CRM_Core_DAO::executeQuery($query, $p);
+        if (trim($contactDetails[1]) != $emailAddress) {
+          civicrm_api3('Email', 'create', [
+            'id' => $emailID,
+            'email' => $emailAddress,
+          ]);
+        }
       }
       else {
         //else insert a new email record
-        $email = new CRM_Core_DAO_Email();
-        $email->contact_id = $contactId;
-        $email->is_primary = 1;
-        $email->email = $emailAddress;
-        $email->save();
-        $emailID = $email->id;
+        $result = civicrm_api3('Email', 'create', [
+          'contact_id' => $contactId,
+          'email' => $emailAddress,
+          'is_primary' => 1,
+        ]);
+        $emailID = $result->id;
       }
 
       CRM_Core_BAO_Log::register($contactId,
@@ -483,7 +479,7 @@ AND    domain_id    = %4
    * @param int $ufID
    *   Id of UF for which related contact_id is required.
    *
-   * @return int|NULL
+   * @return int|null
    *   contact_id on success, null otherwise
    */
   public static function getContactId($ufID) {
@@ -516,7 +512,7 @@ AND    domain_id    = %4
    * @param int $contactID
    *   ID of the contact for which related uf_id is required.
    *
-   * @return int|NULL
+   * @return int|null
    *   uf_id of the given contact_id on success, null otherwise
    */
   public static function getUFId($contactID) {