`OrderExtension` and `OrderFilter` pass string sort directions, deprecated since `doctrine/orm` 3.7
维护者通常 1 天内回复
评估
调研方向
从 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:
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Extension/OrderExtension.php#L72
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Extension/OrderExtension.php#L85
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Filter/OrderFilter.php#L281
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Filter/OrderFilter.php#L284
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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
api-platform/core 的其他 Issue
-
DeserializeProvider calls PartialDenormalizationException::getErrors(), deprecated in Symfony 8.1未关闭
难度 2/5 1-3 小时 新手友好度 76/100
api-platform/core#8650 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
api-platform/core#8649 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
api-platform/core#8647 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 88/100
api-platform/core#8612 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 54/100
api-platform/core#8655 ·
维护者通常 1 天内回复
查看 api-platform/core 的全部 Issue
相似的 Issue
-
📚 Documentation: Placeholder link `link-to-realtime-docs` in Flutter SDK changelog可能已有人在做 @ShyneChikwapulo 今天认领。 未关闭api / realtime product / auth product / messaging product / vcs
难度 1/5 1 小时以内 新手友好度 82/100
appwrite/appwrite#14272 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 1/5 1 小时以内 新手友好度 75/100
Boavizta/boaviztapi#580 · 1 条评论 ·
-
Add Prestashop未关闭request
难度 2/5 1-3 小时 新手友好度 72/100
endoflife-date/endoflife.date#11303 ·
维护者通常 1 天内回复
-
0. to triage enhancement
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复