diff --git a/src/ORM/FieldType/DBText.php b/src/ORM/FieldType/DBText.php index c719a525d..e64d4c421 100644 --- a/src/ORM/FieldType/DBText.php +++ b/src/ORM/FieldType/DBText.php @@ -206,8 +206,8 @@ class DBText extends DBString if ($position > 0) { // We don't want to start mid-word $position = max( - (int) mb_strrpos(substr($text, 0, $position), ' '), - (int) mb_strrpos(substr($text, 0, $position), "\n") + (int) mb_strrpos(mb_substr($text, 0, $position), ' '), + (int) mb_strrpos(mb_substr($text, 0, $position), "\n") ); } diff --git a/src/ORM/Queries/SQLConditionalExpression.php b/src/ORM/Queries/SQLConditionalExpression.php index 32bc44e56..2c651feda 100644 --- a/src/ORM/Queries/SQLConditionalExpression.php +++ b/src/ORM/Queries/SQLConditionalExpression.php @@ -283,6 +283,16 @@ abstract class SQLConditionalExpression extends SQLExpression // array('type' => 'inner', 'table' => 'SiteTree', 'filter' => array("SiteTree.ID = 1", // "Status = 'approved'", 'order' => 20)) if (!is_array($join)) { + if (empty($alias) || is_numeric($alias)) { + continue; + } + + $trimmedAlias = trim($alias, '"'); + + if ($trimmedAlias !== trim($join, '"')) { + $joins[$alias] = "{$join} AS \"{$trimmedAlias}\""; + } + continue; } @@ -321,8 +331,15 @@ abstract class SQLConditionalExpression extends SQLExpression */ protected function getOrderedJoins($from) { + if (count($from) <= 1) { + return $from; + } + // shift the first FROM table out from so we only deal with the JOINs + reset($from); + $baseFromAlias = key($from); $baseFrom = array_shift($from); + $this->mergesort($from, function ($firstJoin, $secondJoin) { if (!is_array($firstJoin) || !is_array($secondJoin) @@ -335,7 +352,12 @@ abstract class SQLConditionalExpression extends SQLExpression }); // Put the first FROM table back into the results - array_unshift($from, $baseFrom); + if (!empty($baseFromAlias) && !is_numeric($baseFromAlias)) { + $from = array_merge([$baseFromAlias => $baseFrom], $from); + } else { + array_unshift($from, $baseFrom); + } + return $from; } diff --git a/tests/php/ORM/SQLSelectTest.php b/tests/php/ORM/SQLSelectTest.php index 391d3d8cb..3ee31c6ce 100755 --- a/tests/php/ORM/SQLSelectTest.php +++ b/tests/php/ORM/SQLSelectTest.php @@ -823,4 +823,28 @@ class SQLSelectTest extends SapphireTest $this->assertEquals(array('%MyName%', '2012-08-08 12:00'), $parameters); $query->execute(); } + + public function testBaseTableAliases() + { + $query = SQLSelect::create('*', ['"MyTableAlias"' => '"MyTable"']); + $sql = $query->sql(); + + $this->assertSQLEquals('SELECT * FROM "MyTable" AS "MyTableAlias"', $sql); + + $query = SQLSelect::create('*', ['MyTableAlias' => '"MyTable"']); + $sql = $query->sql(); + + $this->assertSQLEquals('SELECT * FROM "MyTable" AS "MyTableAlias"', $sql); + + $query = SQLSelect::create('*', ['"MyTableAlias"' => '"MyTable"']); + $query->addLeftJoin('OtherTable', '"Thing" = "OtherThing"', 'OtherTableAlias'); + $sql = $query->sql(); + + $this->assertSQLEquals( + 'SELECT * + FROM "MyTable" AS "MyTableAlias" + LEFT JOIN "OtherTable" AS "OtherTableAlias" ON "Thing" = "OtherThing"', + $sql + ); + } }