Skip to content

Commit

Permalink
(dev/core/98) Searching by any Address fields with location type othe…
Browse files Browse the repository at this point in the history
…r than primary throw DB error
  • Loading branch information
monishdeb committed May 3, 2018
1 parent 90db4f6 commit d567ed1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 13 additions & 1 deletion CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down
5 changes: 4 additions & 1 deletion tests/phpunit/CRM/Contact/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<last_name>, <first_name>' format, as per which the first character would be B of Bond
$this->assertEquals('B', $sortChar[0]['sort_name']);
$this->assertEquals($contactID, key($rows));
}
}
Expand Down

0 comments on commit d567ed1

Please sign in to comment.