From d567ed10debf293eb67d673ff81ce8d9a48ac73e Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Thu, 3 May 2018 11:32:25 +0530 Subject: [PATCH] (dev/core/98) Searching by any Address fields with location type other than primary throw DB error --- CRM/Contact/BAO/Query.php | 14 +++++++++++++- tests/phpunit/CRM/Contact/SelectorTest.php | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 1752cba15243..d5f91b64f69b 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4841,6 +4841,13 @@ public function searchQuery( $additionalFromClause = NULL, $skipOrderAndLimit = FALSE ) { + // In this case we are expecting the search query to return all the first single letter characters of contacts ONLY, + // but when FGB (FULL_GROUP_BY_MODE) is enabled MySQL expect the columns present in GROUP BY must be present in SELECT clause + // and that results into error and needless to have other columns. In order to resolve this disable FGB to fulfill both the cases + if ($sortByChar) { + CRM_Core_DAO::disableFullGroupByMode(); + } + if ($includeContactIds) { $this->_includeContactIds = TRUE; $this->_whereClause = $this->whereClause(); @@ -4899,7 +4906,7 @@ public function searchQuery( if (!empty($groupByCols)) { // It doesn't matter to include columns in SELECT clause, which are present in GROUP BY when we just want the contact IDs - if (!$groupContacts) { + if (!$groupContacts && !$sortByChar) { $select = self::appendAnyValueToSelect($this->_select, $groupByCols, 'GROUP_CONCAT'); } $groupBy = " GROUP BY " . implode(', ', $groupByCols); @@ -4940,6 +4947,11 @@ public function searchQuery( } $dao = CRM_Core_DAO::executeQuery($query); + + if ($sortByChar) { + CRM_Core_DAO::reenableFullGroupByMode(); + } + if ($groupContacts) { $ids = array(); while ($dao->fetch()) { diff --git a/tests/phpunit/CRM/Contact/SelectorTest.php b/tests/phpunit/CRM/Contact/SelectorTest.php index d95ce3ae34d1..200f07b14302 100644 --- a/tests/phpunit/CRM/Contact/SelectorTest.php +++ b/tests/phpunit/CRM/Contact/SelectorTest.php @@ -72,9 +72,12 @@ public function testSelectorQuery($dataSet) { } // Ensure that search builder return individual contact as per criteria if (!empty($dataSet['context'] == 'builder')) { - $contactID = $this->individualCreate(); + $contactID = $this->individualCreate(['first_name' => 'James', 'last_name' => 'Bond']); $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, ''); $this->assertEquals(1, count($rows)); + $sortChar = $selector->alphabetQuery()->fetchAll(); + // sort name is stored in ', ' format, as per which the first character would be B of Bond + $this->assertEquals('B', $sortChar[0]['sort_name']); $this->assertEquals($contactID, key($rows)); } }