mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
b7015cca46
@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user