From 410e3761439737e7e6806276a0c728d7ee87b657 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 4 Nov 2019 14:15:18 +1300 Subject: [PATCH] Move all thte code that relates to joining tables/constructing From to one place. This could definitely be cleaned up more - but the goal of this step is just to move all the code that constructs the from code into one place, so it can be called more sensibly. Tested in the newly written testAddressCustomFields --- CRM/Core/BAO/CustomQuery.php | 40 +++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/CRM/Core/BAO/CustomQuery.php b/CRM/Core/BAO/CustomQuery.php index cd6b5f4039..622200624c 100644 --- a/CRM/Core/BAO/CustomQuery.php +++ b/CRM/Core/BAO/CustomQuery.php @@ -226,32 +226,15 @@ SELECT f.id, f.label, f.data_type, $this->_element["{$name}_id"] = 1; $this->_select[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName"; $this->_element[$fieldName] = 1; - $joinTable = $field['search_table']; $this->joinCustomTableForField($field); - - if ($joinTable) { - $joinClause = 1; - $joinTableAlias = $joinTable; - // Set location-specific query - if (isset($this->_locationSpecificCustomFields[$id])) { - list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields[$id]; - $joinTableAlias = "$locationType-address"; - $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)"; - } - $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id"; - if (!empty($this->_ids[$id])) { - $this->_whereTables[$name] = $this->_tables[$name]; - } - if ($joinTable != 'contact_a') { - $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause; - } - } } } /** * Generate the where clause and also the english language equivalent. + * + * @throws \CRM_Core_Exception */ public function where() { foreach ($this->_ids as $id => $values) { @@ -483,6 +466,25 @@ SELECT f.id, f.label, f.data_type, $join = "\nLEFT JOIN $name ON $name.entity_id = {$field['search_table']}.id"; $this->_tables[$name] = $this->_tables[$name] ?? $join; $this->_whereTables[$name] = $this->_whereTables[$name] ?? $join; + + $joinTable = $field['search_table']; + if ($joinTable) { + $joinClause = 1; + $joinTableAlias = $joinTable; + // Set location-specific query + if (isset($this->_locationSpecificCustomFields[$field['id']])) { + list($locationType, $locationTypeId) = $this->_locationSpecificCustomFields[$field['id']]; + $joinTableAlias = "$locationType-address"; + $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)"; + } + $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id"; + if (!empty($this->_ids[$field['id']])) { + $this->_whereTables[$name] = $this->_tables[$name]; + } + if ($joinTable !== 'contact_a') { + $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause; + } + } } } -- 2.25.1