简体   繁体   English

Symfony 4 Forms:在 QueryBuilder 中为 ChoiceType 字段实现每个实体的自定义排序

[英]Symfony 4 Forms: implementing per-Entity custom sorting in QueryBuilder for ChoiceType fields

I seem to have some troubles getting my head around the intrinsics of the Symfony (4) Form component.我似乎很难理解 Symfony (4) Form 组件的内在函数。

I want to:我想要:

a) store information per-Entity(Repository?) on how the default sort order should be, like $entityRepository->sortOrder = ['default' => ['LastName ASC', 'FirstName ASC'], 'custom1' => [...]]; a) 按实体(存储库?)存储有关默认排序顺序的信息,例如 $entityRepository->sortOrder = ['default' => ['LastName ASC', 'FirstName ASC'], 'custom1' => [...]];

b) use that information to instruct the Form to use that ordering with the query_builder option/attribute for the ChoiceType form field b) 使用该信息指示表单使用该排序与 ChoiceType 表单字段的 query_builder 选项/属性

So the question is, how/where should i ideally store that information and how should i feed this to the Form object / QueryBuilder?所以问题是,我应该如何/在哪里理想地存储这些信息,我应该如何将它提供给表格 object / QueryBuilder?

Pointing me in a direction doing it in a "industry-standard" fashion would be greatly appreciated.以“行业标准”的方式向我指出一个方向,将不胜感激。

Symfony Forms comes with an EntityType that allows you to specify a custom query: Symfony Forms 带有一个允许您指定自定义查询的EntityType

https://symfony.com/doc/4.4/reference/forms/types/entity.html#ref-form-entity-query-builder https://symfony.com/doc/4.4/reference/forms/types/entity.html#ref-form-entity-query-builder

If the ordering is specific to the form, you might want to build the query inline in your form (like in the example of the link above).如果排序特定于表单,您可能希望在表单中内联构建查询(如上面链接的示例中)。 If the same ordering of an entity applies to multiple forms, extract it into a method in your repository and use the method when building the custom query in the form type.如果一个实体的相同排序适用于多个 forms,请将其提取到存储库中的方法中,并在构建表单类型的自定义查询时使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM