Sonata admin - "order by" does not work for Entity

I cannot get sorting entities to work in the admin list of the sonata, here is my entity:

class User extends BaseUser { /** * @ORM\ManyToOne(targetEntity="Region", inversedBy="users") */ protected $preferredRegion; } 

And here is the definition of configureListFields:

  protected function configureListFields(ListMapper $listMapper) { $listMapper->add('preferredRegion', NULL, array('label' => 'Preferred Region', 'sortable' => 'preferredRegion')) } 

When you click on the table heading table to sort it by the name of my object, I get this error:

An exception was thrown during template rendering (line [Semantical Error] 0, col 25 next to "AS __order_by": Error: Entity \ User does not have a field or association with the name AS)

How can I make this class work for an object so that it sorts alphabetically by the name of my entity?

thanks

Julian Manser

+4
source share
1 answer

Try:

 $listMapper->add('preferredRegion.id', NULL, array('label' => 'Preferred Region')); 

If this works, instead of .id you can put .title, .name from another column in this table.

+4
source

Source: https://habr.com/ru/post/1447968/


All Articles