Php 如何在formbuilder中按entityType字段的选项对group_进行排序?
我正在使用frombuilder对$entity输出进行分组:Php 如何在formbuilder中按entityType字段的选项对group_进行排序?,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我正在使用frombuilder对$entity输出进行分组: $options['group_by'] = function ($entity) { if ($entity->getCategory() != null) { return $entity->getCategory()->getName(); } else { return "all"; } }; 这是我的下拉列表的输出: col
$options['group_by'] = function ($entity) {
if ($entity->getCategory() != null) {
return $entity->getCategory()->getName();
} else {
return "all";
}
};
这是我的下拉列表的输出:
colors
blue
red
green
all
colors
friends
friends
elephant
monkey
但我喜欢分组按字母顺序排列。我期望的结果是:
all
colors
friends
colors
blue
red
green
friends
elephant
monkey
我在文档中找不到这样做的任何提示
有机会这样做吗?您可以通过自定义QueryBuilder按组名对选项进行排序:
$builder->add(“…”,EntityType::class[
'class'=>YourEntity::class,
“查询生成器”=>函数(EntityRepository$repository){
返回$repository->createQueryBuilder('实体')
->leftJoin('entity.category','category')
->addSelect('entity.category为NULL时为'all\'其他category.name以隐藏的category\u name'结尾)
->订购人('category_name','ASC')
->addOrderBy('entity.name','ASC');
},
“分组依据”=>。。。,
]);
我尝试了你的代码,但我得到了一个错误
[语法错误]第0行,第90列:错误:预期的已知函数,得到了“IFNULL”
而且我已经在使用查询生成器,我的排序方式如下>orderBy('entity.name','ASC','entity.category.name','ASC')代码>因为名字也必须按字母顺序排列。我已经编辑了我的帖子,请重新测试。(我使用的是来自的IFNULL()
,我忘记了它不是条令中的默认函数。现在由CASE WHEN
替换。)您应该首先按类别名称排序,然后按实体名称排序。