/** * Build a Join using an existing SugarQuery Object * @param Link2 $link * @param SugarQuery $sugar_query * @param Array $options array of additional paramters. Possible parameters include * - 'myAlias' String name of starting table alias * - 'joinTableAlias' String alias to use for the related table in the final result * - 'reverse' Boolean true if this join should be built in reverse for subpanel style queries where the select is * on the related table * - 'ignoreRole' Boolean true if the role column of the relationship should be ignored for this join . * @return SugarQuery */ public function buildJoinSugarQuery(Link2 $link, $sugar_query, $options) { $linkIsLHS = $link->getSide() == REL_LHS; if (!empty($options['reverse'])) { $linkIsLHS = !$linkIsLHS; } $startingTable = $linkIsLHS ? $this->def['lhs_table'] : $this->def['rhs_table']; if (!empty($options['myAlias'])) { $startingTable = $options['myAlias']; } $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key']; $startingJoinKey = $linkIsLHS ? $this->def['join_key_lhs'] : $this->def['join_key_rhs']; $joinTable = $this->getRelationshipTable(); $joinKey = $linkIsLHS ? $this->def['join_key_rhs'] : $this->def['join_key_lhs']; $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table']; $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key']; $targetModule = $linkIsLHS ? $this->def['rhs_module'] : $this->def['lhs_module']; $join_type = isset($options['joinType']) ? $options['joinType'] : 'INNER'; $joinTable_alias = $sugar_query->getJoinTableAlias($joinTable, false, false); $targetTable_alias = !empty($options['joinTableAlias']) ? $options['joinTableAlias'] : $targetTable; $relTableJoin = $sugar_query->joinTable($joinTable, array('alias' => $joinTable_alias, 'joinType' => $join_type, 'linkingTable' => true))->on()->equalsField("{$startingTable}.{$startingKey}", "{$joinTable_alias}.{$startingJoinKey}")->equals("{$joinTable_alias}.deleted", "0"); $targetTableJoin = $sugar_query->joinTable($targetTable, array('alias' => $targetTable_alias, 'joinType' => $join_type, 'bean' => BeanFactory::newBean($targetModule)))->on()->equalsField("{$targetTable_alias}.{$targetKey}", "{$joinTable_alias}.{$joinKey}")->equals("{$targetTable_alias}.deleted", "0"); $sugar_query->join[$targetTable_alias]->relationshipTableAlias = $joinTable_alias; if (empty($options['ignoreRole'])) { $this->buildSugarQueryRoleWhere($sugar_query, $joinTable_alias); } $this->addCustomToSugarQuery($sugar_query, $options, $linkIsLHS, $targetTable_alias); return array($joinTable_alias => $relTableJoin, $targetTable_alias => $targetTableJoin); }