简体   繁体   English

Symfony 2 Sonata Admin - ListMapper中的自定义排序

[英]Symfony 2 Sonata Admin - Custom sort in ListMapper

I have a Customer entity with fields such as FirstName, LastName. 我有一个Customer实体,其中包含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: 我的configureListFields函数如下所示:

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

How can I make the field fullName sortable? 如何使字段fullName可排序?

How can I add custom sort criteria with or without Doctrine ORM? 如何在有或没有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 :-( 问题是它只会对lastName而不是lastName firstName进行排序:-(

You can do it in this way: 你可以这样做:

Entity: 实体:

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

And List Fieds: 并列出Fieds:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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