Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony forms entity/document sorting the property

I'm using the symfony forms for creating a select-box with all my users. I display them by fullname, but want to sort them alphabetically.

$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
    ]);

How can i sort the users alphabetically on fullname of firstName?

like image 478
Mathias Dewelde Avatar asked Mar 17 '14 20:03

Mathias Dewelde


1 Answers

What you need is to add queryBuilder to the form params

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
]);

I assumed the fieldname in your entity is u.fullname

like image 134
Javad Avatar answered Dec 15 '22 10:12

Javad