Skip to content

Commit

Permalink
Merge pull request #17134 from jitendrapurohit/core-1723-rc
Browse files Browse the repository at this point in the history
dev/core#1723 - Adv Search - Reciprocal relationship search with custom fields leads to error
  • Loading branch information
seamuslee001 authored Apr 22, 2020
2 parents eb0c07b + 804261a commit 28d4527
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
6 changes: 3 additions & 3 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 28d4527

Please sign in to comment.