6

I have an Admin class which has this definition of listFields:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
            ->addIdentifier('type')
            ->add('created_at', 'datetime')
            ->add('updated_at', 'datetime')
            ->add('created_by')
            ->add('updated_by')
            ->add('is_active')
            ->add('is_deleted')
            ->add('_action', 'actions',
                    array(
                'actions' => array(
                    'view' => array(),
                    'edit' => array(),
                    'delete' => array()
                )
            ))
    ;

}

Only the "type" column is sortable - IE, when you hover over the table header for "Type" you see an asc/desc arrow and can click to re-order the rows based on this column.

How do I get that to show up on more columns?

I tried adding sortable=true but then it's trying to join to another Entity.

Charles
  • 50,943
  • 13
  • 104
  • 142
Jessica
  • 7,075
  • 28
  • 39
  • Could help : http://stackoverflow.com/questions/8120787/sonata-admin-bundle-order – Léo Benoist Jul 24 '13 at 15:36
  • I've read that one, it's just about setting the default sort field. I want to have sorting on more columns. (Not at the same time, I just want the option to sort other columns instead of the first one) – Jessica Jul 24 '13 at 15:43
  • @Jessica Were you able to solve the issue..?? If yes please share it here. I am facing the same issue. – GBRocks Sep 20 '13 at 10:37
  • Nope, I did not yet. I just came back to it after working on other stuff and googled the error again and found my own thread :-P I cannot do the suggestion to use a specific field in the entity because I am relying on the entity __toString function to print it. I will have to decide to either: A. Use a specific field B. Not sort the field C. Extend Sonata and allow it to sort an entity by the __toString value (which would mean the sorting is in PHP and not MySQL and clearly much slower) – Jessica Sep 27 '13 at 20:12

3 Answers3

12
# we can sort the related entity properties like. This following condition site is an entity

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('site',null,array(
            'sortable'=>true,
            'sort_field_mapping'=> array('fieldName'=>'name'),
            'sort_parent_association_mappings' => array(array('fieldName'=>'site')
            )))
    ;
}

this is the way to sort the related entities in list configuration. Just check this Sort list by an entity field

Gara
  • 627
  • 4
  • 13
4

Sonata will be able to sort a field if it knows what type it is ; if you list a related entity, it will be impossible to sort.

Here is the configureListFields() from an entity "Event" which has a title and is linked to another entity "City".

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
            ->addIdentifier('title')
            ->add('city')
}

A link will be created to the city but it will not be sortable, instead adding a specific field from "City" will work :

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('title')
        ->add('city.name')
}

Now it's sortable.

BatsaxIV
  • 147
  • 7
0

You have to add sortable option on the field.

Here is the code i use:

protected function configureListFields(ListMapper $listMapper) {
    $listMapper
        ->addIdentifier('name')
        ->add('application', null, array('sortable' => true))
        ->add('isActive', null, array('editable' => true))
        ->add('_action', 'actions', array(
            'actions' => array(
                'view' => array(),
                'edit' => array(),
                'delete' => array(),
            )
        ))
    ;
}

Hope this helps

Picoss
  • 2,047
  • 13
  • 14
  • 2
    "I tried adding sortable=true but then it's trying to join to another Entity." – Jessica Jul 25 '13 at 14:09
  • This is the exact error I get using that code Catchable Fatal Error: Argument 1 passed to Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery::entityJoin() must be of the type array, null given, called in /vagrant/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Datagrid/ProxyQuery.php on line 140 and defined in /vagrant/vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Datagrid/ProxyQuery.php line 245 – Jessica Jul 26 '13 at 16:29
  • In my case, the entity linked to the admin didn't contain the field i was listing. The field I'm list is from a child entity. Sonata tries to find metadata about what to do with the field but cant find any because it's from a child class so it tries to find a relation. – Quisse May 10 '19 at 12:52