View sorting list in Sonata Admin by related entity fields

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

Let's say we have 3 objects: city, state and country. All of them have properties idand name. The city has many to one with the state, and the state has many to one with the country. They all have methods __toStringthat display the value of the property name.

We can create a list view for the City object in Sonata Admin as follows:

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

To illustrate, the view may 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                |
|-----||--------------------||--------------------||--------------------|

Id Name, ^ . , show .

:

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

(City-> -> )? :

|-----||--------------------||--------------------||--------------------|
| 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')
    ;
}

, .

, . , ?

+7
1

SonataAdminBundle Symfony . . :

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

sort_field_mapping fieldName, , . . , , , .

sort_parent_association_mappings - . , : City , , , Country.

, .

+14

All Articles