按相关实体字段对 Sonata Admin 中的列表视图进行排序

Sort list view in Sonata Admin by related entity fields

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

假设我们有 3 个实体:城市、州和国家/地区。它们都具有属性 idname。 City 与 State 具有多对一关系,State 与 Country 具有多对一关系。它们都有 __toString 方法来显示 属性 名称的值。

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

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

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

|-----||--------------------||--------------------||--------------------|
| 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 排序,符号 ^ 应该描述这一点。我希望能够按相关实体字段对列表进行排序,并有一个 link 指向相关实体的显示操作。

这是我如何通过 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
))
//...

现在列表视图可以按 属性 name 实体 StateState 列中的所有字段进行排序=47=]状态指向当前状态的显示页面:

|-----||--------------------||--------------------||--------------------|
| 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                |
|-----||--------------------||--------------------||--------------------|

如何按国家(城市->州->国家)对列表视图进行排序?像这样:

|-----||--------------------||--------------------||--------------------|
| 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                |
|-----||--------------------||--------------------||--------------------|

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

//...
->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
))
//...

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

我能做到:

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

并解决了排序问题,但是没有 link 实体。

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

发布问题后的第二天,我正在研究 SonataAdminBundle 和 Symfony 的源代码并找到了解决方案。这实际上很容易。开始了:

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

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

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

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

希望我的解释是通俗易懂的,对其他人也有帮助。