简体   繁体   中英

Symfony 2 Sonata Admin - Custom sort in ListMapper

I have a Customer entity with fields such as FirstName, LastName.

Also I have a method in the entity like this:

public function getFullName() {
        return sprintf("%s %s", $this->getLastName(), $this->getFirstName());
}

My configureListFields function looks like the following:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('fullName')
        ->add('birthday')
        ->add('email')
        ->add('phone')
    ;
}

How can I make the field fullName sortable?

How can I add custom sort criteria with or without Doctrine ORM?

I guess you can hack using a custom template if it is OK to sort only on lastname.

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('last', null, array(
            'sortable' => true,
            'template' => 'YourAdminBundle:CRUD:fullname.html.twig')
        ->add('birthday')
        ->add('email')
        ->add('phone')
    ;
}

in your template :

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
{{ object.lastName }} {{ object.firstName }} 
{% endblock %}

The problem is that it will only sort on lastName and not lastName firstName :-(

You can do it in this way:

Entity:

public function getTextFullName() 
{
   return $this->LastName . $this->FirstName;
}

And List Fieds:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('textFullName')
        ->add('birthday')
        ->add('email')
        ->add('phone')
    ;
}

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