Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

`OrderExtension` and `OrderFilter` pass string sort directions, deprecated since `doctrine/orm` 3.7

未关闭 适合新手
#8,648 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@VincentLanglet 已经在做这个了。

开始于 2026年10月7日。

  • #8654 来自 @VincentLanglet —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
重构
描述清晰度
描述清楚
活跃度
活跃
技术栈
php
领域
backend

调研方向

从 src/Doctrine/Orm/Extension/OrderExtension.php 第 72 行和第 85 行,以及 src/Doctrine/Orm/Filter/OrderFilter.php 第 281 行和第 284 行开始,这些位置将 'ASC'/'DESC' 字面量传给 orderBy()/addOrderBy()。引入一个小的辅助方法,仅在 doctrine/orm >= 3.7 时(通过 Composer\InstalledVersions 或对 QueryBuilder::addOrderBy() 的反射)将其映射为 \SortDirection::Ascending/Descending,否则返回字符串,然后将其应用于全部四个调用点,包括 OrderFilter 的 nulls 处理。完成的定义是:使用 doctrine/orm 3.7 的 collection 请求不出现 string-sort 的 deprecation,且在 ORM 2.17/^3.3 上没有破坏——用现有的 Doctrine ORM extension/filter 测试套件验证。

由索引模型根据 Issue 内容生成。

描述

API Platform affected: 4.3.21, and the current heads of 4.4, 5.0 and main

The doctrine/orm 3.7 deprecates string based sort directions in QueryBuilder::orderBy() and addOrderBy(). It wants the PHP 8.6 \SortDirection enum instead, which ORM itself requires through symfony/polyfill-php86. See "Deprecated using strings or null as sort directions" in https://github.com/doctrine/orm/blob/3.7.x/UPGRADE.md

OrderExtension and OrderFilter still pass 'ASC' and 'DESC' literals:

Line 85 matters most. collection.order defaults to ASC, so every Doctrine collection without its own order gets addOrderBy('o.id', 'ASC'). Any app on doctrine/orm 3.7 sees this on its first collection request:

Passing string as $order to Doctrine\ORM\QueryBuilder::addOrderBy() is deprecated, use an instance of SortDirection instead.
(QueryBuilder.php:1518 called by QueryBuilder.php:1254, https://github.com/doctrine/orm/issues/11313, package doctrine/orm)

The message names QueryBuilder.php as the caller, and doctrine/deprecations reports it only once per process, so it took me a while to trace it back to API Platform. We fixed all our own orderBy() calls and it kept coming.

How to reproduce

PHP 8.4, Symfony 8.1, doctrine/orm 3.7.3, deprecations visible (for example PHPUnit with failOnDeprecation). Request any Doctrine-backed GetCollection.

Possible solution

As the API Platform still allows doctrine/orm ^2.17 || ^3.3, passing the enum unconditionally would break older ORM versions. A small helper could map 'ASC'/'DESC' to \SortDirection::Ascending/Descending when the installed ORM is 3.7 or newer, and return the string otherwise. The version check could use i.e. Composer\InstalledVersions, or reflection on the second parameter of QueryBuilder::addOrderBy(). OrderFilter needs the same mapping, including the nulls handling on line 281.

I could prepare such PR if proposed solution fits. Which branch should it target then, 4.4?

主要语言
PHP
星标
2.6k
派生
987
平均合并
1 天 8 小时
30 天内合并 PR
90

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

api-platform/core 的其他 Issue

查看 api-platform/core 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。