From e07671a8903870b7c7f2d24e446963f9460ef68c Mon Sep 17 00:00:00 2001 From: Serge Latyntcev Date: Mon, 6 May 2019 15:19:21 +1200 Subject: [PATCH] Fix SQLConditionalExpression::getJoins so it always adds explicit aliases This is a revert of https://github.com/silverstripe/silverstripe-framework/pull/8956 --- src/ORM/Queries/SQLConditionalExpression.php | 9 +-------- tests/php/ORM/SQLSelectTest.php | 7 +++++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/ORM/Queries/SQLConditionalExpression.php b/src/ORM/Queries/SQLConditionalExpression.php index 012504daf..dc551d879 100644 --- a/src/ORM/Queries/SQLConditionalExpression.php +++ b/src/ORM/Queries/SQLConditionalExpression.php @@ -287,14 +287,7 @@ abstract class SQLConditionalExpression extends SQLExpression continue; } - if (preg_match('/AS\s+(?:"[^"]+"|[A-Z0-9_]+)\s*$/i', $join ?? '')) { - // custom aliases override the ones defined through array keys - // this is only meant to keep backward compatibility with SS <= 4.3, - // to be removed in SS5 - continue; - } - - $trimmedAlias = trim($alias ?? '', '"'); + $trimmedAlias = trim($alias, '"'); if ($trimmedAlias !== trim($join ?? '', '"')) { $joins[$alias] = "{$join} AS \"{$trimmedAlias}\""; diff --git a/tests/php/ORM/SQLSelectTest.php b/tests/php/ORM/SQLSelectTest.php index f3618ebe0..426099ac7 100755 --- a/tests/php/ORM/SQLSelectTest.php +++ b/tests/php/ORM/SQLSelectTest.php @@ -814,15 +814,18 @@ class SQLSelectTest extends SapphireTest $sql ); + // This feature is a bug that used to exist in SS4 and was removed in SS5 + // so now we test it does not exist and we end up with incorrect SQL because of that + // In SS4 the "explicitAlias" would be ignored $query = SQLSelect::create('*', [ 'MyTableAlias' => '"MyTable"', - 'ignoredAlias' => ', (SELECT * FROM "MyTable" where "something" = "whatever") as "CrossJoin"' + 'explicitAlias' => ', (SELECT * FROM "MyTable" where "something" = "whatever") as "CrossJoin"' ]); $sql = $query->sql(); $this->assertSQLEquals( 'SELECT * FROM "MyTable" AS "MyTableAlias" , ' . - '(SELECT * FROM "MyTable" where "something" = "whatever") as "CrossJoin"', + '(SELECT * FROM "MyTable" where "something" = "whatever") as "CrossJoin" AS "explicitAlias"', $sql ); }