Skip to content

Commit 29d55d8

Browse files
committed
Fixed SQL error when sorting by custom fields or Maria
1 parent 55583e6 commit 29d55d8

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes for Craft CMS 5
22

3+
## Unreleased
4+
5+
- Fixed a SQL error that could occur when sorting by custom fields on MariaDB.
6+
37
## 5.2.7 - 2024-07-16
48

59
- `craft\helpers\UrlHelper::actionUrl()` now returns URLs based on the primary site’s base URL (if it has one), for console requests if the `@web` alias wasn’t explicitly defined.

src/services/ElementSources.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use craft\models\FieldLayout;
2424
use Illuminate\Support\Collection;
2525
use yii\base\Component;
26+
use yii\db\Expression;
2627

2728
/**
2829
* The Element Sources service provides APIs for managing element indexes.
@@ -327,10 +328,14 @@ public function getSourceSortOptions(string $elementType, string $sourceKey): ar
327328
->groupBy('attribute')
328329
->map(function(Collection $group) {
329330
$orderBys = $group->pluck('orderBy');
330-
if ($orderBys->count() === 1 || $orderBys->doesntContain(fn($orderBy) => is_string($orderBy))) {
331+
if ($orderBys->doesntContain(fn($orderBy) => is_string($orderBy))) {
331332
return $group->first();
332333
}
333-
$expression = new CoalesceColumnsExpression($orderBys->all());
334+
if ($orderBys->count() === 1) {
335+
$expression = new Expression($orderBys->first());
336+
} else {
337+
$expression = new CoalesceColumnsExpression($orderBys->all());
338+
}
334339
return array_merge($group->first(), [
335340
'orderBy' => $expression,
336341
]);

0 commit comments

Comments
 (0)