QueryBuilder->{orderBy/addOrderBy} do not support instances of Order enum type #7537

Closed
opened 2026-01-22 15:53:09 +01:00 by admin · 0 comments
Owner

Originally created by @wayne530 on GitHub (Jul 24, 2025).

Feature Request

Recently, a new deprecation warning started showing for the old Criteria::ASC and Criteria::DESC constants, suggesting that Order::Ascending and Order::Descending should be used, respectively. This does work fine when using Criteria::create()->orderBy, but it does not work for QueryBuilder->orderBy or QueryBuilder->addOrderBy, which generates the following exception:

[2025-07-24T17:56:56.346871+00:00] request.CRITICAL: Uncaught PHP Exception Error: "Object of class Doctrine\Common\Collections\Order could not be converted to string" at OrderBy.php line 52 {"exception":"[object] (Error(code: 0): Object of class Doctrine\\Common\\Collections\\Order could not be converted to string at...

In order to get around this, we must explicitly use Order::Ascending->value or Order::Descending->value, which is quite a bit more verbose. Seems like it could potentially make sense to add this support into QueryBuilder and it seems relatively straightforward as well.

What

Support for:

$qb = $this->createQueryBuilder('e');
$qb->addOrderBy('e.someField', Order::Ascending);

instead of:

$qb = $this->createQueryBuilder('e');
$qb->addOrderBy('e.someField', Order::Ascending->value);

Why

It's more concise and other interfaces that provide ordering for queries support it (Criteria::create()->orderBy).

How

The simplest way would be to modify addOrderBy and orderBy of the QueryBuilder class and allow them to accept instances of Order. Update Expr\OrderBy's constructor to also accept instances or Order and convert them to their corresponding string value internally.

Originally created by @wayne530 on GitHub (Jul 24, 2025). ### Feature Request Recently, a new deprecation warning started showing for the old `Criteria::ASC` and `Criteria::DESC` constants, suggesting that `Order::Ascending` and `Order::Descending` should be used, respectively. This does work fine when using `Criteria::create()->orderBy`, but it does not work for `QueryBuilder->orderBy` or `QueryBuilder->addOrderBy`, which generates the following exception: ``` [2025-07-24T17:56:56.346871+00:00] request.CRITICAL: Uncaught PHP Exception Error: "Object of class Doctrine\Common\Collections\Order could not be converted to string" at OrderBy.php line 52 {"exception":"[object] (Error(code: 0): Object of class Doctrine\\Common\\Collections\\Order could not be converted to string at... ``` In order to get around this, we must explicitly use `Order::Ascending->value` or `Order::Descending->value`, which is quite a bit more verbose. Seems like it could potentially make sense to add this support into QueryBuilder and it seems relatively straightforward as well. #### What Support for: ``` $qb = $this->createQueryBuilder('e'); $qb->addOrderBy('e.someField', Order::Ascending); ``` instead of: ``` $qb = $this->createQueryBuilder('e'); $qb->addOrderBy('e.someField', Order::Ascending->value); ``` #### Why It's more concise and other interfaces that provide ordering for queries support it (`Criteria::create()->orderBy`). #### How The simplest way would be to modify `addOrderBy` and `orderBy` of the `QueryBuilder` class and allow them to accept instances of `Order`. Update `Expr\OrderBy`'s constructor to also accept instances or `Order` and convert them to their corresponding string value internally.
admin closed this issue 2026-01-22 15:53:09 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7537