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

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

问题描述

使用Sonata Admin Bundle,这是Symfony的一个很好的附件,我遇到了以下问题:



假设我们有3个实体:城市,州和国家。他们都有属性 id 名称。城市与国家有多对一的关系,州与国家有多对一的关系。他们都必须使用显示属性名称值的字符串方法。



我们可以在Sonata Admin中为实体城市创建列表视图,如下所示:



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



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

  | ----- || -------------------- || ----------------- --- || -------------------- | 
| Id ^ ||名称^ ||状态||州国家|
| ----- || -------------------- || ---------------- ---- || -------------------- |
| 1 ||纽约||纽约||美国|
| 2 ||阿卡普尔科||格雷罗||墨西哥|
| 3 ||卡尔加里||艾伯塔省||加拿大|
| 4 ||蒂华纳||下加利福尼亚州||墨西哥|
| 5 ||温哥华||不列颠哥伦比亚省||加拿大|
| 6 ||洛杉矶||加州||美国|
| ----- || -------------------- || ---------------- ---- || -------------------- |

根据默认列表,可以按照 ID 名称,符号^应该描述。我希望能够通过相关实体字段对列表进行排序,并指向相关实体的显示操作的链接。



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

  // ... 
- > add 'state',null,array(
'route'=> array('name'=>'show'),
'sortable'=> true,
'sort_field_mapping' = array('fieldName'=>'name'),//实体状态的属性名称
'sort_parent_association_mappings'=>数组(array('fieldName'=>'state'))//实体的财产状态City
))
// ...

现在列表视图可以通过实体状态的属性名称排序,列状态中的所有字段指向当前状态的显示页面:

  | ----- || ----------------- --- || -------------------- || -------------------- | 
| Id ^ ||名称^ ||状态^ ||州国家|
| ----- || -------------------- || ---------------- ---- || -------------------- |
| 3 ||卡尔加里||艾伯塔省||加拿大|
| 4 ||蒂华纳||下加利福尼亚州||墨西哥|
| 5 ||温哥华||不列颠哥伦比亚省||加拿大|
| 6 ||洛杉矶||加州||美国|
| 2 ||阿卡普尔科||格雷罗||墨西哥|
| 1 ||纽约||纽约||美国|
| ----- || -------------------- || ---------------- ---- || -------------------- |

如何通过国家(City->状态 - >国家)?这样的事情:

  | ----- || -------------- ------ || -------------------- || -------------------- | 
| Id ^ ||名称^ ||状态^ ||州国家|
| ----- || -------------------- || ---------------- ---- || -------------------- |
| 3 ||卡尔加里||艾伯塔省||加拿大|
| 5 ||温哥华||不列颠哥伦比亚省||加拿大|
| 2 ||阿卡普尔科||格雷罗||墨西哥|
| 4 ||蒂华纳||下加利福尼亚州||墨西哥|
| 6 ||洛杉矶||加州||美国|
| 1 ||纽约||纽约||美国|
| ----- || -------------------- || ---------------- ---- || -------------------- |

当我尝试像上面的代码片段一样:



pre> // ...
- > add('state.country',null,array(
'route'=> array(' name'=>'show'),
'sortable'=> true,
'sort_field_mapping'=>数组('fieldName'=>'country.name'),//属性实体名称国家/地区
'sort_parent_association_mappings'=>数组(array('fieldName'=>'state.country'))//实体状态的属性国家
))
// ...

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



我可以做:

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

并解决排序问题,但是没有链接到实体。



官方文档非常好,但是缺少这个主题。那么,如何通过分层实体对列表视图进行排序?

解决方案

发布问题后的第二天我正在挖掘源SonataAdminBundle和Symfony的代码找到了解决方案。其实很简单。这里是:

  // ... 
- > add(
'state.country' ,
null,
数组(
'associated_property'=>'name',// 实体的属性名称国家
'sortable'=> true ,// 重要!使列可排序
'sort_field_mapping'=>数组(
'fieldName'=>'name'// 实体的属性名称国家
),
'sort_parent_association_mappings'=> array(
array('fieldName'=>'state')// 实体城市的财产状态
数组('fieldName'=>'country')// 实体状态的属性国家



//。

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



选项 sort_field_mapping 需要数组与键值 fieldName 保存我们正在排序的属性。这里我们按国家的名字排序。我们可以根据人口排序,假设我们在实体国家拥有该属性,尽管我们显示了该名称的值。



sort_parent_association_mappings 是最有趣的部分。这里我们定义应该创建连接查询的属性:城市有一个属性状态,它是实体状态,它本身的属性国家是实体国家。



<我希望我的解释是可以理解的,也可以帮助别人。


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

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 to string methods displaying the value of the property name.

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

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

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

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.

I could do:

  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?

解决方案

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

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.

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.

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天全站免登陆