From: Seamus Lee Date: Tue, 21 Dec 2021 04:41:23 +0000 (+1100) Subject: dev/core#3000 Fix database error when copying custom data fields from a master addres... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a1fc8261ce33edfcbd907fa2024dd92877dc7976;p=civicrm-core.git dev/core#3000 Fix database error when copying custom data fields from a master address to related addresses Add in hook param as per jon --- diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index f7c8ca8ecc..e2644ab539 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -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'])) { diff --git a/CRM/Core/BAO/CustomValueTable.php b/CRM/Core/BAO/CustomValueTable.php index 8556c4ec21..555de5f9f9 100644 --- a/CRM/Core/BAO/CustomValueTable.php +++ b/CRM/Core/BAO/CustomValueTable.php @@ -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']] = []; } diff --git a/tests/phpunit/api/v3/AddressTest.php b/tests/phpunit/api/v3/AddressTest.php index 10362fb75f..b8acd353c0 100644 --- a/tests/phpunit/api/v3/AddressTest.php +++ b/tests/phpunit/api/v3/AddressTest.php @@ -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']]); + } + }