From 1dd81488aace798dc196610e75240b452e392ce9 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Wed, 7 Feb 2024 17:38:14 +0100 Subject: [PATCH 1/3] fix unknown table Fixes https://github.com/silverstripe/silverstripe-framework/issues/10737 --- src/ORM/DataQuery.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index 303117066..27feb838c 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -298,13 +298,19 @@ class DataQuery $collisionClassColumn = $schema->sqlColumnForField($collisionClass, 'ClassName'); $collisionClasses = ClassInfo::subclassesFor($collisionClass); $collisionClassesSQL = implode(', ', Convert::raw2sql($collisionClasses, true)); - $caseClauses[] = "WHEN {$collisionClassColumn} IN ({$collisionClassesSQL}) THEN $collision"; + // Only add clause if this is already joined "Unknown column 'ClassName' error" + $collisionTable = $schema->tableForField($collisionClass, 'ClassName'); + if (array_key_exists($collisionTable, $query->getFrom())) { + $caseClauses[] = "WHEN {$collisionClassColumn} IN ({$collisionClassesSQL}) THEN $collision"; + } } } else { user_error("Bad collision item '$collision'", E_USER_WARNING); } } - $query->selectField("CASE " . implode(" ", $caseClauses) . " ELSE NULL END", $collisionField); + if (!empty($caseClauses)) { + $query->selectField("CASE " . implode(" ", $caseClauses) . " ELSE NULL END", $collisionField); + } } } From 2ac68a55a4a806a64459b744f76a49e37367ee0d Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Thu, 8 Feb 2024 09:13:40 +0100 Subject: [PATCH 2/3] rename var --- src/ORM/DataQuery.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index 27feb838c..73417ffdd 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -299,8 +299,8 @@ class DataQuery $collisionClasses = ClassInfo::subclassesFor($collisionClass); $collisionClassesSQL = implode(', ', Convert::raw2sql($collisionClasses, true)); // Only add clause if this is already joined "Unknown column 'ClassName' error" - $collisionTable = $schema->tableForField($collisionClass, 'ClassName'); - if (array_key_exists($collisionTable, $query->getFrom())) { + $collisionTableForClassName = $schema->tableForField($collisionClass, 'ClassName'); + if (array_key_exists($collisionTableForClassName, $query->getFrom())) { $caseClauses[] = "WHEN {$collisionClassColumn} IN ({$collisionClassesSQL}) THEN $collision"; } } From e0107e0202914edd25629443a044b6883c18bb83 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Thu, 8 Feb 2024 09:14:27 +0100 Subject: [PATCH 3/3] Update src/ORM/DataQuery.php Co-authored-by: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> --- src/ORM/DataQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index 73417ffdd..f13f1afdd 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -298,7 +298,7 @@ class DataQuery $collisionClassColumn = $schema->sqlColumnForField($collisionClass, 'ClassName'); $collisionClasses = ClassInfo::subclassesFor($collisionClass); $collisionClassesSQL = implode(', ', Convert::raw2sql($collisionClasses, true)); - // Only add clause if this is already joined "Unknown column 'ClassName' error" + // Only add clause if this is already joined to avoid "Unknown column 'ClassName'" error $collisionTableForClassName = $schema->tableForField($collisionClass, 'ClassName'); if (array_key_exists($collisionTableForClassName, $query->getFrom())) { $caseClauses[] = "WHEN {$collisionClassColumn} IN ({$collisionClassesSQL}) THEN $collision";