1
votes

There is the following code

    $form->with('Item')->add('parent', null, array(
                                        'label' => 'Category',
                                        'required' => true,
                                        'query_builder' => 
                                        function($er) use ($id) {
                                            $qb = $er->createQueryBuilder('p');
                                                if ($id){
                                                    $qb->where('p.id <> :id')
                                                       ->setParameter('id', $id);
                                                }
                                            $qb->orderBy('p.root, p.lft', 'ASC');
                                            return $qb;
                                         }
                                       .........

Result is the entity-objects collection which is given to the string (__toString method). It return name-field. But I need get the another field - url.

How to get url value instead the name in the select-list form? The query_builder type return object => how to change this form that it works likewise query_builder?

2
ok, but can I replace the query_builder type with another? - user3342506
@bigmax, sorry, your answer is right, I misunderstood the documentation. - user3342506

2 Answers

2
votes

I didn't work with SonataAdminBundle forms, but I think that it works absolutely like symfony forms. All that you need here is to add 'class' and 'property' values to your options list:

$form->with('Item')->add('parent', null, array(
    'class' => 'Acme\DemoBundle\Entity\Category',
    'property' => 'url',
    'label' => 'Category',
    'required' => true,
    'query_builder' => 
    function($er) use ($id) {
        $qb = $er->createQueryBuilder('p');
            if ($id){
                $qb->where('p.id <> :id')
                   ->setParameter('id', $id);
            }
        $qb->orderBy('p.root, p.lft', 'ASC');
        return $qb;
     }

property - is the name of the field in your entity that will represent your Entity's value instead of calling __toString(). But also... If you need always represent your Entity as URL you can simply override __toString() method in the Entity class to something like that:

public function __toString() {
    return $this->url;
}
0
votes

In my case, ->orderBy() used in query_builder worked, but was overwritten for unknown reasons "somewhere" later. Fun fact: when I used extended => true, everything was rendered like sorted in the query_builder. But by using extended => false my <option>s are re-sorted before select2 touched it.

As a workaround I did this:

config/packages/twig.yaml

twig:
    paths:
        '%kernel.project_dir%/templates': '%kernel.project_dir%/templates'
        # This is to prevent a infinite loop when extending the parent template
        'vendor/sonata-project/admin-bundle/src/Resources/views': SonataAdminBundleOriginal

And then I done the sorting I wanted again in twig for the project entity:

templates/bundles/SonataAdminBundle/Form/form_admin_fields.html.twig:

{% extends '@SonataAdminBundleOriginal/Form/form_admin_fields.html.twig' %}

{%- block choice_widget_options -%}
    {% if name == 'project' %}
        {% set options = options|sort((a, b) => a.data.numberSortable <=> b.data.numberSortable)|reverse %}
    {% endif %}

    {{ parent() }}
{%- endblock choice_widget_options -%}