Skip to content

Commit

Permalink
CRM-21806: Fix issues is search form when FULL_GROUP_BY_MODE enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Mar 6, 2018
1 parent d1c0c69 commit decefc2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
13 changes: 11 additions & 2 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4872,7 +4872,13 @@ public function searchQuery(

$order = $orderBy = $limit = '';
if (!$count) {
list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
if (!$groupContacts) {
list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
}
// always GROUP BY on contact IDs when $groupContacts = TRUE i.e. when we want return contact IDs only.
else {
$groupByCols = array('contact_a.id');
}

if ($rowCount > 0 && $offset >= 0) {
$offset = CRM_Utils_Type::escape($offset, 'Int');
Expand All @@ -4890,7 +4896,10 @@ public function searchQuery(
list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted);

if (!empty($groupByCols)) {
$select = self::appendAnyValueToSelect($this->_select, $groupByCols, 'GROUP_CONCAT');
// 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) {
$select = self::appendAnyValueToSelect($this->_select, $groupByCols, 'GROUP_CONCAT');
}
$groupBy = " GROUP BY " . implode(', ', $groupByCols);
if (!empty($order)) {
// retrieve order by columns from ORDER BY clause
Expand Down
5 changes: 2 additions & 3 deletions CRM/Contact/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,6 @@ public function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = self::C
$sql = $this->_query->searchQuery($start, $end, $sort, FALSE, $this->_query->_includeContactIds,
FALSE, TRUE, TRUE);
}
$replaceSQL = $this->_query->getSelect();

// CRM-9096
// due to limitations in our search query writer, the above query does not work
Expand All @@ -1025,10 +1024,10 @@ public function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = self::C

$insertSQL = "
INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
SELECT DISTINCT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
SELECT DISTINCT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.sort_name
";

$sql = str_replace($replaceSQL, $insertSQL, $sql);
$sql = str_replace(array("SELECT contact_a.id as contact_id", "SELECT contact_a.id as id"), $insertSQL, $sql);
try {
CRM_Core_DAO::executeQuery($sql);
}
Expand Down

0 comments on commit decefc2

Please sign in to comment.