From 9f170f284434ecd949c8c1d8bb863e350b32ccd3 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Wed, 28 Aug 2024 14:19:37 +0100 Subject: [PATCH 1/3] when ordering by value from json col, cast text to char --- src/base/Field.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/base/Field.php b/src/base/Field.php index 591207913f0..1618686a0f6 100644 --- a/src/base/Field.php +++ b/src/base/Field.php @@ -773,15 +773,31 @@ public function getPreviewHtml(mixed $value, ElementInterface $element): string */ public function getSortOption(): array { - if (static::dbType() === null || !isset($this->layoutElement)) { + $dbType = static::dbType(); + if ($dbType === null || !isset($this->layoutElement)) { throw new NotSupportedException('getSortOption() not supported by ' . $this->name); } + $orderBy = $this->getValueSql(); + + // for mysql, we have to make sure text column type is cast to char, otherwise it won't be sorted correctly + // see https://github.com/craftcms/cms/issues/15609 + $db = Craft::$app->getDb(); + if ($db->getIsMysql()) { + $castType = match (Db::parseColumnType($dbType)) { + Schema::TYPE_TEXT => 'CHAR(255)', + default => null, + }; + if ($castType !== null) { + $orderBy = "CAST($orderBy AS $castType)"; + } + } + // The attribute name should match the table attribute name, // per ElementSources::getTableAttributesForFieldLayouts() return [ 'label' => Craft::t('site', $this->name), - 'orderBy' => $this->getValueSql(), + 'orderBy' => $orderBy, 'attribute' => isset($this->layoutElement->handle) ? "fieldInstance:{$this->layoutElement->uid}" : "field:$this->uid", From 13b3b3b360007d2aadfcf426287772976bb8c84a Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Thu, 29 Aug 2024 14:33:34 -0700 Subject: [PATCH 2/3] Cleanup --- src/base/Field.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/base/Field.php b/src/base/Field.php index 1618686a0f6..9392459f5f8 100644 --- a/src/base/Field.php +++ b/src/base/Field.php @@ -783,14 +783,8 @@ public function getSortOption(): array // for mysql, we have to make sure text column type is cast to char, otherwise it won't be sorted correctly // see https://github.com/craftcms/cms/issues/15609 $db = Craft::$app->getDb(); - if ($db->getIsMysql()) { - $castType = match (Db::parseColumnType($dbType)) { - Schema::TYPE_TEXT => 'CHAR(255)', - default => null, - }; - if ($castType !== null) { - $orderBy = "CAST($orderBy AS $castType)"; - } + if ($db->getIsMysql() && Db::parseColumnType($dbType) === Schema::TYPE_TEXT) { + $orderBy = "CAST($orderBy AS CHAR(255))"; } // The attribute name should match the table attribute name, From ca827dcfc24a978751ee0689c6a1f1c7d456c287 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Thu, 29 Aug 2024 14:36:06 -0700 Subject: [PATCH 3/3] Release note [ci skip] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6622e9274a1..2b5ad270981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Fixed a bug where tabs within field layout designers weren’t always getting positioned correctly when wrapped. ([#15590](https://github.com/craftcms/cms/issues/15590)) - Fixed a bug where editable table rows’ action buttons were misaligned for newly-created rows. ([#15602](https://github.com/craftcms/cms/issues/15602)) - Fixed a bug where relational fields’ element query results weren’t limited to the selected relations if the `id` param was overridden. ([#15570](https://github.com/craftcms/cms/issues/15570)) +- Fixed a bug where ordering element queries by textual custom fields would factor in character marks. ([#15609](https://github.com/craftcms/cms/issues/15609)) ## 5.3.6 - 2024-08-26