简体   繁体   中英

Sonata admin bundle: order entity field in showMapper

I'm searching for the solution in a development within a Symfony project with Sonata admin bundle.

2 entities: Contacts and Tags (ManyToMany Relationship)

Within the showaction of the sonata admin (tags entity), I'm unable to order the contacts field. It does not matter if I use the query option within the field or not. I used the query option in the create form, there the query works very well and the items are sorted in the select field.

This works (create form)

    protected function configureFormFields(FormMapper $formMapper)
{
    $TagsQuery = $this->modelManager

        ->getEntityManager('TelRegBundle:Tags')
        ->createQuery(
            'SELECT t
             FROM TelRegBundle:Tags t
             ORDER BY t.title ASC'
        );       

    $formMapper
          //...

            ->add('tags', 'sonata_type_model', array(
                'class' => 'TelRegBundle\Entity\Tags',                    
                'property' => 'title',
                'multiple' => true,
                'query' => $TagsQuery,
            ))

Where the same approach does not work (show action):

    protected function configureShowFields(ShowMapper $showMapper)
{      

    $showMapper
        ->with('Tags data')
            ->add('title')
        ->end()
        ->with('Contacts')
            ->add('contacts', 'entity', array(
                'class' => 'TelRegBundle:Contacts',
                'associated_property' => 'title',

                'query' => ...  //Querybuilder not working.

                ))
        ->end()                
   ;
}

Anyone who can help? Thx!

You can only set annotation

/**
 * @ManyToMany(targetEntity="Tags")
 * @OrderBy({"title" = "ASC"})
 */
private $tags;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM