From c239c911fe427acbbf59a33fd26c3fefe6cad05c Mon Sep 17 00:00:00 2001 From: i-just Date: Mon, 20 Apr 2026 11:29:10 +0200 Subject: [PATCH] empty username should be ordered as last --- src/elements/db/UserQuery.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/elements/db/UserQuery.php b/src/elements/db/UserQuery.php index 517f0aac2dc..3c99018ee45 100644 --- a/src/elements/db/UserQuery.php +++ b/src/elements/db/UserQuery.php @@ -50,11 +50,7 @@ class UserQuery extends ElementQuery /** * @inheritdoc */ - protected array $defaultOrderBy = [ - 'users.username' => SORT_ASC, - 'users.active' => SORT_DESC, - 'users.pending' => SORT_DESC, - ]; + protected array $defaultOrderBy; // General parameters // ------------------------------------------------------------------------- @@ -244,6 +240,20 @@ class UserQuery extends ElementQuery */ public bool $withGroups = false; + /** + * @inheritdoc + */ + public function __construct(string $elementType, array $config = []) + { + parent::__construct($elementType, $config); + + $this->defaultOrderBy = [ + new Expression('CASE WHEN [[users.username]] IS NULL THEN 1 ELSE 0 END ASC'), + 'users.active' => SORT_DESC, + 'users.pending' => SORT_DESC, + ]; + } + /** * @inheritdoc */