}
// create employee of relationship
- $relationshipParams = [
- 'is_active' => TRUE,
- 'relationship_type_id' => $relTypeId . '_a_b',
- 'contact_check' => [$organization => TRUE],
- ];
[$duplicate, $relationshipIds]
- = self::legacyCreateMultiple($relationshipParams, $contactID);
+ = self::legacyCreateMultiple($relTypeId, $organization, $contactID);
// In case we change employer, clean previous employer related records.
if (!$previousEmployerID && !$newContact) {
// set current employer
self::setCurrentEmployer([$contactID => $organization]);
- $relationshipParams['relationship_ids'] = $relationshipIds;
+ $relationshipParams = [
+ 'relationship_ids' => $relationshipIds,
+ 'is_active' => 1,
+ 'contact_check' => [$organization => TRUE],
+ 'relationship_type_id' => $relTypeId . '_a_b',
+ ];
// Handle related memberships. CRM-3792
self::currentEmployerRelatedMembership($contactID, $organization, $relationshipParams, $duplicate, $previousEmployerID);
}
* For multiple a new variant of this function needs to be written and migrated to as this is a bit
* nasty
*
- * @param array $params
- * (reference ) an assoc array of name/value pairs.
+ * @param int $relationshipTypeID
+ * @param int $organization
* @param int $contactID
*
* @return array
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
- private static function legacyCreateMultiple(&$params, int $contactID) {
- $invalid = $duplicate = 0;
+ private static function legacyCreateMultiple(int $relationshipTypeID, int $organization, int $contactID): array {
+ $params = [
+ 'is_active' => TRUE,
+ 'relationship_type_id' => $relationshipTypeID . '_a_b',
+ 'contact_check' => [$organization => TRUE],
+ ];
$ids = ['contact' => $contactID];
$relationshipIds = [];
$contactFields = CRM_Contact_BAO_Relationship::setContactABFromIDs($params, $ids, $organizationID);
$errors = CRM_Contact_BAO_Relationship::checkValidRelationship($contactFields, $ids, $organizationID);
if ($errors) {
- $invalid++;
- continue;
+ return [0, []];
}
if (
$organizationID
)
) {
- $duplicate++;
- continue;
+ return [1, []];
}
$singleInstanceParams = array_merge($params, $contactFields);
$relationshipIds[] = $relationship->id;
}
- // do not add to recent items for import, CRM-4399
- if (!(!empty($params['skipRecentView']) || $invalid || $duplicate)) {
- CRM_Contact_BAO_Relationship::addRecent($params, $relationship);
- }
+ CRM_Contact_BAO_Relationship::addRecent($params, $relationship);
- return [$duplicate, $relationshipIds];
+ return [0, $relationshipIds];
}
/**
$this->assertNotContains('Alabama', $result['values']);
}
- public function testUpdateSharedAddressWithCustomFields() {
+ /**
+ * Ensure an update to the second address doesn't cause error.
+ *
+ * Avoid "db error: already exists" when re-saving the custom fields.
+ */
+ public function testUpdateSharedAddressWithCustomFields(): void {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
-
$params = $this->_params;
- $params['custom_' . $ids['custom_field_id']] = "custom string";
-
+ $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']]);
}