Skip to content

Allow SortDirection in EntityRepository::findBy() and findOneBy() - #12613

Open
GromNaN wants to merge 2 commits into
doctrine:3.8.xfrom
GromNaN:sort-direction-repository
Open

GromNaN wants to merge 2 commits into
doctrine:3.8.xfrom
GromNaN:sort-direction-repository

Conversation

@GromNaN

@GromNaN GromNaN commented Sep 13, 2026

Copy link
Copy Markdown
Member

EntityRepository::findBy() and findOneBy() now accept and document \SortDirection values for $orderBy, while 'asc'/'desc' strings keep working without a deprecation.

The ObjectRepository interface of doctrine/persistence 3.x and 4.x documents strings as the only valid order values, so the string form stays accepted on the repository API. As an implementer of the interface, EntityRepository can widen its documented parameter type, so passing \SortDirection::Ascending or \SortDirection::Descending is now allowed and documented there, matching what QueryBuilder, Expr\OrderBy and the mapping API already accept. Native signatures are unchanged.

Breaking change

This is a static-analysis BC break for custom repositories that override findBy() or findOneBy() and forward $orderBy to parent::*(): the documented $orderBy type now also accepts \SortDirection, so an override whose docblock reproduces the previous type (the ObjectRepository string union for findBy(), or array<string, string> for findOneBy()) is reported as incompatible by static analysis. Widen the override docblock accordingly. ObjectRepository itself is unchanged.

Static analysis only allows SortDirection when the repository is typed as EntityRepository (or a subclass), not as Doctrine\Persistence\ObjectRepository, until doctrine/persistence 5 is required. The deprecation scope is clarified in UPGRADE.md accordingly.

Changes:

  • widen the $orderBy phpdoc of EntityRepository::findBy() and findOneBy(), and of EntityPersister::load()
  • add functional tests and a static-analysis fixture
  • update UPGRADE.md and the reference docs

@GromNaN GromNaN changed the title Allow SortDirection in EntityRepository::findBy() and findOneBy() Allow SortDirection in EntityRepository::findBy() and findOneBy() Sep 13, 2026
@GromNaN
GromNaN force-pushed the sort-direction-repository branch 4 times, most recently from 583cc15 to dc62639 Compare September 13, 2026 21:11
Comment thread UPGRADE.md Outdated
Comment thread UPGRADE.md Outdated
@greg0ire

Copy link
Copy Markdown
Member

Since this contains a breaking change, maybe we should mute the issue on 3.7.x, and this PR should target 3.8.x? On the other hand, it's just phpdoc/static analysis. I'm on the fence…

@GromNaN
GromNaN changed the base branch from 3.7.x to 3.8.x September 15, 2026 11:06
Repository methods must keep accepting 'asc'/'desc' strings without a
deprecation because the ObjectRepository interface of doctrine/persistence
3.x and 4.x documents strings as the only order values. As an implementer,
EntityRepository can still widen its documented parameter type, so
\SortDirection is now accepted and documented there as well. This makes the
new SortDirection guidance usable on the most common repository API.

The persister interface load() is aligned to the same widened type.

Add functional tests (with and without second level cache), static analysis
coverage, and clarify the deprecation scope in UPGRADE.md and the reference
docs.
Address review feedback on doctrine#12613:
- drop the ambiguous "assumed" and the PHPStan-specific argument.type
  identifier; reword the BC break to match the QueryBuilder wording
- note that doctrine/persistence 5.0 will accept SortDirection in
  ObjectRepository
@GromNaN
GromNaN force-pushed the sort-direction-repository branch from dc62639 to 6b77782 Compare September 15, 2026 14:12
@GromNaN GromNaN added this to the 3.8.0 milestone Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants