diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 5dab66c8d560..727e5fc2eaa7 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4191,7 +4191,7 @@ public function relationship(&$values) { if (self::$_relType == 'reciprocal') { $where = []; self::$_relationshipTempTable = $relationshipTempTable = CRM_Utils_SQL_TempTable::build() - ->createWithColumns("`contact_id` int(10) unsigned NOT NULL DEFAULT '0', `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0', relationship_id int unsigned, KEY `contact_id` (`contact_id`), KEY `contact_id_alt` (`contact_id_alt`)") + ->createWithColumns("`contact_id` int(10) unsigned NOT NULL DEFAULT '0', `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0', id int unsigned, KEY `contact_id` (`contact_id`), KEY `contact_id_alt` (`contact_id_alt`)") ->getName(); if ($nameClause) { $where[$grouping][] = " sort_name $nameClause "; @@ -4207,7 +4207,7 @@ public function relationship(&$values) { $groupJoinTable = "contact_b"; $groupJoinColumn = "id"; } - $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE); + $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE, 'label', FALSE); if ($nameClause || !$targetGroup) { if (!empty($relationType)) { $relQill = ''; @@ -4315,7 +4315,7 @@ public function relationship(&$values) { $whereClause = str_replace('contact_b', 'c', $whereClause); } $sql = " - INSERT INTO {$relationshipTempTable} (contact_id, contact_id_alt, relationship_id) + INSERT INTO {$relationshipTempTable} (contact_id, contact_id_alt, id) (SELECT contact_id_b as contact_id, contact_id_a as contact_id_alt, civicrm_relationship.id FROM civicrm_relationship INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 00b2c6c708dd..ad0e3f54caf2 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -658,6 +658,54 @@ public function testNonReciprocalRelationshipTargetGroupIsCorrectResults() { $this->assertEquals($contactID_b, $dao->contact_id, "Search query returns parent of contact A?"); } + /** + * Relationship search with custom fields. + */ + public function testReciprocalRelationshipWithCustomFields() { + $params = [ + 'extends' => 'Relationship', + ]; + $customGroup = $this->customGroupCreate($params); + $customFieldId = $this->customFieldCreate(['custom_group_id' => $customGroup['id']])['id']; + $contactID_a = $this->individualCreate(); + $contactID_b = $this->individualCreate(); + $relationship = $this->callAPISuccess('Relationship', 'create', [ + 'contact_id_a' => $contactID_a, + 'contact_id_b' => $contactID_b, + 'relationship_type_id' => 2, + 'is_active' => 1, + "custom_{$customFieldId}" => 'testvalue', + ]); + $params = [ + [ + 0 => 'relation_type_id', + 1 => 'IN', + 2 => + [ + 0 => '2_a_b', + ], + 3 => 0, + 4 => 0, + ], + [ + 0 => "custom_{$customFieldId}", + 1 => '=', + 2 => 'testvalue', + 3 => 0, + 4 => 0, + ], + ]; + + $query = new CRM_Contact_BAO_Query($params); + $dao = $query->searchQuery(); + $this->assertEquals('2', $dao->N); + $this->callAPISuccess('Relationship', 'delete', ['id' => $relationship['id']]); + $this->callAPISuccess('Contact', 'delete', ['id' => $contactID_a, 'skip_undelete' => 1]); + $this->callAPISuccess('Contact', 'delete', ['id' => $contactID_b, 'skip_undelete' => 1]); + $this->callAPISuccess('CustomField', 'delete', ['id' => $customFieldId, 'skip_undelete' => 1]); + $this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup]); + } + /** * @throws \CRM_Core_Exception */