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