From 92ab0047816644442610d263f13b27390b153d06 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 23 Jul 2010 04:32:16 +0000 Subject: [PATCH] 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 --- core/model/SQLQuery.php | 22 ++++++++++++++++++---- tests/SQLQueryTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/core/model/SQLQuery.php b/core/model/SQLQuery.php index 20a1f4fba..acf2391a5 100755 --- a/core/model/SQLQuery.php +++ b/core/model/SQLQuery.php @@ -146,20 +146,34 @@ class SQLQuery { /** * 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 */ - public function leftJoin($table, $onPredicate) { - $this->from[$table] = "LEFT JOIN \"$table\" ON $onPredicate"; + public function leftJoin($table, $onPredicate, $tableAlias=null) { + if( !$tableAlias ) { + $tableAlias = $table; + } + $this->from[$tableAlias] = "LEFT JOIN \"$table\" AS \"$tableAlias\" ON $onPredicate"; return $this; } /** * 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 */ - public function innerJoin($table, $onPredicate) { - $this->from[$table] = "INNER JOIN \"$table\" ON $onPredicate"; + public function innerJoin($table, $onPredicate, $tableAlias=null) { + if( !$tableAlias ) { + $tableAlias = $table; + } + $this->from[$tableAlias] = "INNER JOIN \"$table\" AS \"$tableAlias\" ON $onPredicate"; return $this; } diff --git a/tests/SQLQueryTest.php b/tests/SQLQueryTest.php index dd921c0b3..44ae1a4a1 100644 --- a/tests/SQLQueryTest.php +++ b/tests/SQLQueryTest.php @@ -208,6 +208,30 @@ class SQLQueryTest extends SapphireTest { "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 {