ENHANCEMENT Added argument to SQLQuery->leftJoin()/innerJoin() (#5802, thanks stojg)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@108418 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-07-23 04:32:16 +00:00 committed by Sam Minnee
parent fa65f5edce
commit 92ab004781
2 changed files with 42 additions and 4 deletions

View File

@ -146,20 +146,34 @@ class SQLQuery {
/** /**
* Add a LEFT JOIN criteria to the FROM clause. * Add a LEFT JOIN criteria to the FROM clause.
* *
* @param String $table Table name (unquoted)
* @param String $onPredicate The "ON" SQL fragment in a "LEFT JOIN ... AS ... ON ..." statement.
* Needs to be valid (quoted) SQL.
* @param String $tableAlias Optional alias which makes it easier to identify and replace joins later on
* @return SQLQuery This instance * @return SQLQuery This instance
*/ */
public function leftJoin($table, $onPredicate) { public function leftJoin($table, $onPredicate, $tableAlias=null) {
$this->from[$table] = "LEFT JOIN \"$table\" ON $onPredicate"; if( !$tableAlias ) {
$tableAlias = $table;
}
$this->from[$tableAlias] = "LEFT JOIN \"$table\" AS \"$tableAlias\" ON $onPredicate";
return $this; return $this;
} }
/** /**
* Add an INNER JOIN criteria to the FROM clause. * Add an INNER JOIN criteria to the FROM clause.
* *
* @param String $table Table name (unquoted)
* @param String $onPredicate The "ON" SQL fragment in a "LEFT JOIN ... AS ... ON ..." statement.
* Needs to be valid (quoted) SQL.
* @param String $tableAlias Optional alias which makes it easier to identify and replace joins later on
* @return SQLQuery This instance * @return SQLQuery This instance
*/ */
public function innerJoin($table, $onPredicate) { public function innerJoin($table, $onPredicate, $tableAlias=null) {
$this->from[$table] = "INNER JOIN \"$table\" ON $onPredicate"; if( !$tableAlias ) {
$tableAlias = $table;
}
$this->from[$tableAlias] = "INNER JOIN \"$table\" AS \"$tableAlias\" ON $onPredicate";
return $this; return $this;
} }

View File

@ -208,6 +208,30 @@ class SQLQueryTest extends SapphireTest {
"filtersOnFK() is true with table and quoted column name " "filtersOnFK() is true with table and quoted column name "
); );
} }
public function testInnerJoin() {
$query = new SQLQuery();
$query->from( 'MyTable' );
$query->innerJoin( 'MyOtherTable', 'MyOtherTable.ID = 2' );
$query->leftJoin( 'MyLastTable', 'MyOtherTable.ID = MyLastTable.ID' );
$this->assertEquals( 'SELECT * FROM MyTable '.
'INNER JOIN "MyOtherTable" AS "MyOtherTable" ON MyOtherTable.ID = 2 '.
'LEFT JOIN "MyLastTable" AS "MyLastTable" ON MyOtherTable.ID = MyLastTable.ID',
$query->sql()
);
$query = new SQLQuery();
$query->from( 'MyTable' );
$query->innerJoin( 'MyOtherTable', 'MyOtherTable.ID = 2', 'table1' );
$query->leftJoin( 'MyLastTable', 'MyOtherTable.ID = MyLastTable.ID', 'table2' );
$this->assertEquals( 'SELECT * FROM MyTable '.
'INNER JOIN "MyOtherTable" AS "table1" ON MyOtherTable.ID = 2 '.
'LEFT JOIN "MyLastTable" AS "table2" ON MyOtherTable.ID = MyLastTable.ID',
$query->sql()
);
}
} }
class SQLQueryTest_DO extends DataObject implements TestOnly { class SQLQueryTest_DO extends DataObject implements TestOnly {