按相关实体字段排序 Sonata Admin 中的列表视图 [英] Sort list view in Sonata Admin by related entity fields

查看:17
本文介绍了按相关实体字段排序 Sonata Admin 中的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Sonata Admin Bundle,它是 Symfony 的一个很好的附加组件,我遇到了如下描述的问题.

Using Sonata Admin Bundle, which is a great add-on for Symfony, I have bumped into the problem described as follows.

假设我们有 3 个实体:City、State 和 Country.它们都具有 idname 属性.City 与 State 是多对一的关系,State 与 Country 是多对一的关系.它们都有显示属性名称值的 __toString 方法.

Let's say we have 3 entities: City, State and Country. They all have the properties id and name. City has a many-to-one relation to State and State has a many-to-one relation to Country. They all have __toString methods displaying the value of the property name.

我们可以像这样在 Sonata Admin 中为实体 City 创建一个列表视图:

We can create a list view for the entity City in Sonata Admin like this:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('id')
        ->add('name')
        ->add('state')
        ->add('state.country')
    ;
}

为了说明,视图可能如下所示:

For illustration the view could look like this:

|-----||--------------------||--------------------||--------------------|
| Id ^|| Name ^             || State              || State Country      |
|-----||--------------------||--------------------||--------------------|    
| 1   || New York           || New York           || USA                |
| 2   || Acapulco           || Guerrero           || Mexico             |
| 3   || Calgary            || Alberta            || Canada             |
| 4   || Tijuana            || Baja California    || Mexico             |
| 5   || Vancouver          || British Columbia   || Canada             |
| 6   || Los Angeles        || California         || USA                |
|-----||--------------------||--------------------||--------------------|

默认情况下,该列表可按 IdName 列排序,符号 ^ 应说明这一点.我希望能够按相关实体字段对列表进行排序,并有一个指向相关实体的显示操作的链接.

Per default the list is sortable by the columns Id and Name, the sign ^ should depict that. I would like to be able to sort the list by the related entity fields and have a link pointing to the show action for the related entity.

这是我如何实现按状态排序的:

Here is how I have achieved the sorting by State:

//...
->add('state', null, array(
    'route' => array('name' => 'show'),
    'sortable' => true,
    'sort_field_mapping' => array('fieldName' => 'name'), // property name of entity State
    'sort_parent_association_mappings' => array(array('fieldName' => 'state')) // property state of entity City
))
//...

现在列表视图可以按实体 State 的属性 name 排序,并且 State 列中的所有字段都指向显示页面对于当前状态:

Now the list view is sortable by the property name of the entity State and all fields in the column State point to the show page for the current state:

|-----||--------------------||--------------------||--------------------|
| Id ^|| Name ^             || State ^            || State Country      |
|-----||--------------------||--------------------||--------------------|    
| 3   || Calgary            || Alberta            || Canada             |
| 4   || Tijuana            || Baja California    || Mexico             |
| 5   || Vancouver          || British Columbia   || Canada             |
| 6   || Los Angeles        || California         || USA                |
| 2   || Acapulco           || Guerrero           || Mexico             |
| 1   || New York           || New York           || USA                |
|-----||--------------------||--------------------||--------------------|

如何按Country(City->State->Country)对列表视图进行排序?像这样的:

How do I sort the list view by the Country (City->State->Country)? Something like this:

|-----||--------------------||--------------------||--------------------|
| Id ^|| Name ^             || State ^            || State Country      |
|-----||--------------------||--------------------||--------------------|    
| 3   || Calgary            || Alberta            || Canada             |
| 5   || Vancouver          || British Columbia   || Canada             |
| 2   || Acapulco           || Guerrero           || Mexico             |
| 4   || Tijuana            || Baja California    || Mexico             |
| 6   || Los Angeles        || California         || USA                |
| 1   || New York           || New York           || USA                |
|-----||--------------------||--------------------||--------------------|

当我尝试类似上面的代码片段时:

When I try something like the above code snippet:

//...
->add('state.country', null, array(
    'route' => array('name' => 'show'),
    'sortable' => true,
    'sort_field_mapping' => array('fieldName' => 'country.name'), // property name of entity Country
    'sort_parent_association_mappings' => array(array('fieldName' => 'state.country')) // property country of entity State
))
//...

然后抛出异常错误.我尝试了不同的组合,但都没有成功.

then an exception error is thrown. I tried different combinations, but all without success.

我能做到:

  protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('id')
        ->add('name')
        ->add('state.name')
        ->add('state.country.name')
    ;
}

并解决了排序问题,但没有指向实体的链接.

and get the sorting issue solved, but then there are no links to the entities.

官方文档很好,但是缺少这个话题.那么,如何按层次实体对列表视图进行排序呢?

The official documentation is very good, but is missing this topic. So, how to sort a list view by hierarchical entities?

推荐答案

发布问题后的第二天,我在挖掘 SonataAdminBundle 和 Symfony 的源代码并找到了解决方案.这实际上很容易.就是这样:

The next day after posting the question I was digging around the source code of SonataAdminBundle and Symfony and found the solution. It is actually very easy. Here it goes:

//...
->add(
    'state.country',
    null,
    array(
        'associated_property' => 'name', // property name of entity Country
        'sortable' => true, // IMPORTANT! make the column sortable
        'sort_field_mapping' => array(
            'fieldName' => 'name' // property name of entity Country
        ),
        'sort_parent_association_mappings' => array(
            array('fieldName' => 'state'), // property state of entity City
            array('fieldName' => 'country') // property country of entity State
        )
    )
)
//...

使用 associated_property 我们设置应该显示的属性.如果我们在实体中定义了 __toString 方法,则可以省略.在这种情况下,这意味着国家名称将显示在列中.

With associated_property we set the property that should be displayed. This can be omitted if we have defined a __toString method in the entity. In this case it means the name of the country will be displayed in the column.

sort_field_mapping 选项需要一个数组,其中的键 fieldName 包含我们排序所依据的属性.在这里,我们按国家名称排序.然而,我们可以按人口排序,假设我们在实体 Country 中有该属性,尽管我们正在显示名称的值.

The option sort_field_mapping requires an array with the key fieldName holding the property by which we're sorting. Here we sort by the country's name. We could however sort by population, assuming we have that property in the entity Country, although we're displaying the value for the name.

sort_parent_association_mappings 是最有趣的部分.这里我们定义了应该用来创建连接查询的属性: City 有一个属性 state,它是实体 State,它本身的属性 country 是实体 Country.

And sort_parent_association_mappings is the most interesting part. Here we define the properties by which the join query should be created: City has a property state, which is the entity State, which itself has the property country being the entity Country.

我希望我的解释是可以理解的,也可以帮助其他人.

I hope my explanation is comprehensible and can help other people too.

这篇关于按相关实体字段排序 Sonata Admin 中的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆