我正在使用symfony表单为我的所有用户创建一个选择框.我按全名显示它们,但想按字母顺序排序.
$builder->add('transferTo', 'document', [ 'class' => 'UserBundle:User', 'property' => 'fullname', 'label' => 'Overdragen aan', 'attr' => ['class' => 'form-control selectpicker'], 'label_attr' => ['class' => 'col-sm-2 control-label'], 'multiple' => false, 'required' => true ]);
如何在firstName的全名上按字母顺序对用户进行排序?
你需要的是将queryBuilder添加到表单参数
use Doctrine\ORM\EntityRepository; $builder->add('transferTo', 'document', [ 'class' => 'UserBundle:User', 'query_builder' => function(EntityRepository $repository) { return $repository->createQueryBuilder('u')->orderBy('u.fullname', 'ASC'); } 'property' => 'fullname', 'label' => 'Overdragen aan', 'attr' => ['class' => 'form-control selectpicker'], 'label_attr' => ['class' => 'col-sm-2 control-label'], 'multiple' => false, 'required' => true ]);
我假设你的实体中的字段名是 u.fullname