/** * @covers Query::joinOnce */ function testJoinOnce() { $q = new Query('table'); $q->join('table.column', 'table2.column'); $q->joinOnce('table.column', 'table2.column'); $q->joinOnce('table2', 'column = column'); $joins = $q->getJoins(); $this->assertCount(1, $joins); $join = array_shift($joins); $this->assertEquals('JOIN `table2` ON (`table`.`column` = `table2`.`column`)', $join->getQueryStatement() . ''); }
/** * Build SQL FROM clause * @param Query $query * @param Table $table * @param string &$columns * @return string */ protected function buildFromClause(Query $query, Table $table, &$columns) { $from = $table->getFullIdentifier(); foreach ($query->getJoins() as $join) { if ($join['type'] == 'left') { $from .= "\n\tLEFT JOIN "; } else { $from .= "\n\tINNER JOIN "; } $rightSideTable = $table->getDatabase()->getTable($join['table']); if (!empty($join['columns'])) { foreach ($join['columns'] as $columnName) { $column = $rightSideTable->getColumn($columnName); $columns .= ",\n\t{$column->getSQLExpression()}"; } } $from .= $rightSideTable->getFullIdentifier(); if (empty($join['alias'])) { $rightSideTableReference = $rightSideTable->getFullIdentifier(); } else { $rightSideTableReference = $join['alias']; $from .= ' AS ' . $join['alias']; } $on = ''; foreach ($join['on'] as $leftSide => $rightSide) { $on .= ' AND ' . $table->getFullIdentifier() . '.' . $leftSide . ' = ' . $rightSideTableReference . '.' . $rightSide; } $from .= "\n\t\tON (" . substr($on, 5) . ')'; } return $from; }