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 */