dev/core#3000 Fix database error when copying custom data fields from a master addres...
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 21 Dec 2021 04:41:23 +0000 (15:41 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 23 Dec 2021 20:48:58 +0000 (07:48 +1100)
Add in hook param as per jon

CRM/Core/BAO/Address.php
CRM/Core/BAO/CustomValueTable.php
tests/phpunit/api/v3/AddressTest.php

index f7c8ca8ecc3ca461c11eae347b489dbfafb50f95..e2644ab53905ff5879c27884943e001e6f1e100d 100644 (file)
@@ -78,7 +78,7 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
     if ($address->id) {
       // first get custom field from master address if any
       if (isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) {
-        $address->copyCustomFields($params['master_id'], $address->id);
+        $address->copyCustomFields($params['master_id'], $address->id, $hook);
       }
 
       if (isset($params['custom'])) {
index 8556c4ec217b11812661f23ce4725864a2da51f2..555de5f9f9f5e00550dee1c6a30b0d9a988ba367 100644 (file)
@@ -376,6 +376,13 @@ class CRM_Core_BAO_CustomValueTable {
         if (!empty($customValue['id'])) {
           $cvParam['id'] = $customValue['id'];
         }
+        elseif (empty($cvParam['is_multiple']) && !empty($entityID)) {
+          // dev/core#3000 Ensure that if we are not dealing with multiple record custom data and for some reason have got here without getting the id of the record in the custom table for this entityId let us give it one last shot
+          $rowId = CRM_Core_DAO::singleValueQuery("SELECT id FROM {$cvParam['table_name']} WHERE entity_id = %1", [1 => [$entityID, 'Integer']]);
+          if (!empty($rowId)) {
+            $cvParam['id'] = $rowId;
+          }
+        }
         if (!array_key_exists($customValue['table_name'], $cvParams)) {
           $cvParams[$customValue['table_name']] = [];
         }
index 10362fb75f97c1ddeb15035a075520846e00748b..b8acd353c02322f0f684dc0553e15a81d2d990ab 100644 (file)
@@ -609,4 +609,21 @@ class api_v3_AddressTest extends CiviUnitTestCase {
     $this->assertNotContains('Alabama', $result['values']);
   }
 
+  public function testUpdateSharedAddressWithCustomFields() {
+    $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
+
+    $params = $this->_params;
+    $params['custom_' . $ids['custom_field_id']] = "custom string";
+
+    $firstAddress = $this->callAPISuccess($this->_entity, 'create', $params);
+
+    $contactIdB = $this->individualCreate();
+
+    $secondAddressParams = array_merge(['contact_id' => $contactIdB, 'master_id' => $firstAddress['id']], $firstAddress);
+    unset($secondAddressParams['id']);
+    $secondAddress = $this->callAPISuccess('Address', 'create', $secondAddressParams);
+    // Ensure an update to the second address doesn't cause a "db error: already exists" when resaving the custom fields.
+    $this->callAPISuccess('Address', 'create', ['id' => $secondAddress['id'], 'contact_id' => $contactIdB, 'master_id' => $firstAddress['id']]);
+  }
+
 }