How to get the defined field in sonataadmin query builder?
Asked Answered
M

3

1

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?

Morisco answered 25/2, 2014 at 11:57 Comment(3)
symfony.com/doc/current/reference/forms/types/…Syrian
ok, but can I replace the query_builder type with another?Morisco
@bigmax, sorry, your answer is right, I misunderstood the documentation.Morisco
G
2

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;
}
Gelhar answered 27/2, 2014 at 16:53 Comment(0)
P
0

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 -%}
Photojournalism answered 29/4, 2020 at 12:23 Comment(0)
T
0

Just in case if someone needs to "filter" an entity by a FK (foreign key) just like i needed (and searched for 3 days), here is the solution:

In the __construct you can set/find what you need. In my case i work this "sessions" (year). So i find the session->status=true:

$this->sessionActive = $this->sessionRepository->findOneBy(['status'=>true]);

Then in the QueryBuilder:

protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
    {
        $rootAlias = current($query->getRootAliases());
        $query
            ->select($rootAlias)
            ->where($rootAlias.'.session = :id')
            ->addOrderBy($rootAlias.'.dateStart', 'ASC')
            ->setParameter('id', $this->sessionActive->getId());
        return $query;
    }

Note: I have always one Session->status set to "true". If you might have null values don't forget to make a condition so that it doesn't give you an error!

Regards.

Tweezers answered 9/7, 2022 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.