/** * Create a from statement * * @param SugarBean|array $bean * @return string */ protected function compileFrom($bean) { $alias = ""; $return = array(); if (is_array($bean)) { list($bean, $alias) = $bean; $this->from_alias = $alias; } $this->from_bean = $bean; $table = $bean->getTableName(); $table_cstm = ''; $from_clause = "{$table}"; if (!empty($alias)) { $from_clause .= " {$alias}"; } //SugarQuery will determine if we actually need to add the table or not. $this->sugar_query->joinCustomTable($bean, $alias); if (!empty($this->from_alias)) { $this->primary_table = $this->from_alias; $this->primary_custom_table = $this->from_alias . '_c'; } else { $this->primary_table = $this->from_bean->getTableName(); $this->primary_custom_table = $this->from_bean->get_custom_table_name(); } $return = $from_clause; return $return; }
function build_report_query_join($name, $alias, $parentAlias, SugarBean $module, $type, $query = array(), SugarBean $rel_module = null) { if (!isset($query['join'][$alias])) { switch ($type) { case 'custom': $query['join'][$alias] = 'LEFT JOIN ' . $this->db->quoteIdentifier($module->get_custom_table_name()) . ' ' . $this->db->quoteIdentifier($name) . ' ON ' . $module->table_name . '.id = ' . $this->db->quoteIdentifier($name) . '.id_c '; break; case 'relationship': if ($module->load_relationship($name)) { $params['join_type'] = 'LEFT JOIN'; if ($module->{$name}->relationship_type != 'one-to-many') { if ($module->{$name}->getSide() == REL_LHS) { $params['right_join_table_alias'] = $this->db->quoteIdentifier($alias); $params['join_table_alias'] = $this->db->quoteIdentifier($alias); $params['left_join_table_alias'] = $this->db->quoteIdentifier($parentAlias); } else { $params['right_join_table_alias'] = $this->db->quoteIdentifier($parentAlias); $params['join_table_alias'] = $this->db->quoteIdentifier($alias); $params['left_join_table_alias'] = $this->db->quoteIdentifier($alias); } } else { $params['right_join_table_alias'] = $this->db->quoteIdentifier($parentAlias); $params['join_table_alias'] = $this->db->quoteIdentifier($alias); $params['left_join_table_alias'] = $this->db->quoteIdentifier($parentAlias); } $linkAlias = $parentAlias . "|" . $alias; $params['join_table_link_alias'] = $this->db->quoteIdentifier($linkAlias); $join = $module->{$name}->getJoin($params, true); $query['join'][$alias] = $join['join']; if ($rel_module != null) { $query['join'][$alias] .= $this->build_report_access_query($rel_module, $name); } $query['select'][] = $join['select'] . " AS '" . $alias . "_id'"; } break; default: break; } } return $query; }
function build_report_query_join($name, SugarBean $module, $type, $query = array()) { if (!isset($query['join'][$name])) { switch ($type) { case 'custom': $query['join'][$name] = 'LEFT JOIN ' . $module->get_custom_table_name() . ' ' . $name . ' ON ' . $module->table_name . '.id = ' . $name . '.id_c '; break; case 'relationship': if ($module->load_relationship($name)) { $params['join_type'] = 'LEFT JOIN'; $params['join_table_alias'] = $name; $join = $module->{$name}->getJoin($params, true); $query['join'][$name] = $join['join']; $query['select'][] = $join['select'] . " AS '" . $name . "_id'"; } break; default: break; } } return $query; }
/** * Joins the custom table to the current query (if possible) * @param SugarBean $bean * @param string $alias */ public function joinCustomTable($bean, $alias = "") { if ($bean->hasCustomFields() && !$this->customJoined) { $table = $bean->getTableName(); $table_cstm = $bean->get_custom_table_name(); if (!empty($table_cstm)) { // TODO: CLEAN THIS UP if (!empty($alias)) { $sql = "LEFT JOIN {$table_cstm} {$alias}_c ON {$alias}_c.id_c = {$alias}.id"; } else { $sql = "LEFT JOIN {$table_cstm} ON {$table_cstm}.id_c = {$table}.id"; } // can do a join here because we haven't got to the joins yet in the compile sequence. $this->joinRaw($sql); } } }