From ee3924702fea42d178ec682f95bbc972e10b8a23 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 21 Mar 2023 16:02:54 +0000 Subject: [PATCH] MDL-77705 reportbuilder: avoid re-using field alias between entities. Ensure that when the user entity is added multiple times to a report, when there are custom profile fields, each of those gets a unique table alias per-entity. --- .../classes/local/helpers/user_profile_fields.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/reportbuilder/classes/local/helpers/user_profile_fields.php b/reportbuilder/classes/local/helpers/user_profile_fields.php index bed8d1e9c7d..66f15b83eb9 100644 --- a/reportbuilder/classes/local/helpers/user_profile_fields.php +++ b/reportbuilder/classes/local/helpers/user_profile_fields.php @@ -115,11 +115,21 @@ class user_profile_fields { /** * Generate table alias for given profile field * + * The entity name is used to ensure the alias differs when the entity is used multiple times within the same report, each + * having their own table alias/join + * * @param profile_field_base $profilefield * @return string */ private function get_table_alias(profile_field_base $profilefield): string { - return "upfs{$profilefield->fieldid}"; + static $aliases = []; + + $aliaskey = "{$this->entityname}_{$profilefield->fieldid}"; + if (!array_key_exists($aliaskey, $aliases)) { + $aliases[$aliaskey] = database::generate_alias(); + } + + return $aliases[$aliaskey]; } /**