From 1cc1711f3495ba33c2d3552e70bf5bd550cb64c2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 10 Feb 2023 14:40:55 -0800 Subject: [PATCH] Disambiguate `Address.state_province_id:abbr` (MySQL; simpler version) Consider `ContactJoinTest::testCreateWithPrimaryAndBilling` which writes the value: 'address_billing.state_province_id:abbr' => 'AK', The symbol 'AK' can map to three places: Akwa Ibom (Nigeria), Atakora (Benin), and Alaska (USA). This is an ambiguous choice. It should be resolved in a consistent way. One flavor of ambiguity comes from MySQL. When loading abbreviations, Civi queries with `ORDER BY abbreviation`. This is a *typically* stable, but it has no *guaranteed* resolution. Adding a secondary sort key makes the outcome clear/unambiguous. --- CRM/Core/PseudoConstant.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 4e3b97c0c1..3159f67725 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -1500,6 +1500,10 @@ WHERE id = %1 $from = 'FROM %3'; $wheres = []; $order = 'ORDER BY %2'; + if (in_array('id', $availableFields, TRUE)) { + // Example: 'ORDER BY abbreviation, id' because `abbreviation`s are not unique. + $order .= ', id'; + } // Use machine name in certain contexts if ($context === 'validate' || $context === 'match') { -- 2.25.1