From f946d15245964ed5a960d73045c6792b5d9bbc44 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 2 Mar 2018 13:53:46 +1300 Subject: [PATCH] CRM-21806 Search builder NOT Empty does not work --- CRM/Contact/BAO/Query.php | 20 +++++++++++++++----- CRM/Contact/Selector.php | 16 ++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index ac156fdf3a..4dfcde1e36 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1444,11 +1444,7 @@ class CRM_Contact_BAO_Query { } } - $select = "SELECT "; - if (isset($this->_distinctComponentClause)) { - $select .= "{$this->_distinctComponentClause}, "; - } - $select .= implode(', ', $this->_select); + $select = $this->getSelect(); $from = $this->_fromClause; } @@ -6542,4 +6538,18 @@ AND displayRelType.is_active = 1 return FALSE; } + /** + * Get Select Clause. + * + * @return string + */ + public function getSelect() { + $select = "SELECT "; + if (isset($this->_distinctComponentClause)) { + $select .= "{$this->_distinctComponentClause}, "; + } + $select .= implode(', ', $this->_select); + return $select; + } + } diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index c9a163017a..d0f5b4a85a 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -1005,15 +1005,14 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se // For custom searches, use the contactIDs method if (is_a($this, 'CRM_Contact_Selector_Custom')) { $sql = $this->_search->contactIDs($start, $end, $sort, TRUE); - $replaceSQL = "SELECT contact_a.id as contact_id"; $coreSearch = FALSE; } // For core searches use the searchQuery method else { $sql = $this->_query->searchQuery($start, $end, $sort, FALSE, $this->_query->_includeContactIds, FALSE, TRUE, TRUE); - $replaceSQL = "SELECT contact_a.id as id"; } + $replaceSQL = $this->_query->getSelect(); // CRM-9096 // due to limitations in our search query writer, the above query does not work @@ -1030,20 +1029,17 @@ SELECT DISTINCT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', cont "; $sql = str_replace($replaceSQL, $insertSQL, $sql); - - $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); - $result = CRM_Core_DAO::executeQuery($sql); - unset($errorScope); - - if (is_a($result, 'DB_Error')) { - // check if we get error during core search + try { + CRM_Core_DAO::executeQuery($sql); + } + catch (CRM_Core_Exception $e) { if ($coreSearch) { // in the case of error, try rebuilding cache using full sql which is used for search selector display // this fixes the bugs reported in CRM-13996 & CRM-14438 $this->rebuildPreNextCache($start, $end, $sort, $cacheKey); } else { - // return if above query fails + CRM_Core_Session::setStatus(ts('Query Failed')); return; } } -- 2.25.1