Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when ordering by value from json col, cast text to char #15615

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions src/base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,15 +773,25 @@ 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() && Db::parseColumnType($dbType) === Schema::TYPE_TEXT) {
$orderBy = "CAST($orderBy AS CHAR(255))";
}

// 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",
Expand Down