Merge pull request #3297 from senorgeno/3282-orm-left-join-sub-select

BUG #3282: Added ability to subselect with in left or inner join
This commit is contained in:
Simon Welsh 2014-07-18 19:15:39 +10:00
commit b7015cca46
2 changed files with 25 additions and 2 deletions

View File

@ -903,9 +903,11 @@ class SQLQuery {
else if(sizeof($join['filter']) == 1) $filter = $join['filter'][0];
else $filter = "(" . implode(") AND (", $join['filter']) . ")";
$table = strpos(strtoupper($join['table']), 'SELECT') ? $join['table'] : "\""
. $join['table'] . "\"";
$aliasClause = ($alias != $join['table']) ? " AS \"" . Convert::raw2sql($alias) . "\"" : "";
$this->from[$alias] = strtoupper($join['type']) . " JOIN \""
. $join['table'] . "\"$aliasClause ON $filter";
$this->from[$alias] = strtoupper($join['type']) . " JOIN "
. $table . "$aliasClause ON $filter";
}
}

View File

@ -342,6 +342,27 @@ class SQLQueryTest extends SapphireTest {
);
}
public function testJoinSubSelect() {
$query = new SQLQuery();
$query->setFrom('MyTable');
$query->addInnerJoin('(SELECT * FROM MyOtherTable)',
'Mot.MyTableID = MyTable.ID', 'Mot');
$query->addLeftJoin('(SELECT MyLastTable.MyOtherTableID, COUNT(1) as MyLastTableCount FROM MyLastTable '
. 'GROUP BY MyOtherTableID)',
'Mlt.MyOtherTableID = Mot.ID', 'Mlt');
$query->setOrderBy('COALESCE(Mlt.MyLastTableCount, 0) DESC');
$this->assertEquals('SELECT *, COALESCE(Mlt.MyLastTableCount, 0) AS "_SortColumn0" FROM MyTable '.
'INNER JOIN (SELECT * FROM MyOtherTable) AS "Mot" ON Mot.MyTableID = MyTable.ID ' .
'LEFT JOIN (SELECT MyLastTable.MyOtherTableID, COUNT(1) as MyLastTableCount FROM MyLastTable '
. 'GROUP BY MyOtherTableID) AS "Mlt" ON Mlt.MyOtherTableID = Mot.ID ' .
'ORDER BY "_SortColumn0" DESC',
$query->sql()
);
}
public function testSetWhereAny() {
$query = new SQLQuery();
$query->setFrom('MyTable');