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'),
'sort_parent_association_mappings' => array(array('fieldName' => 'state'))
))
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'),
'sort_parent_association_mappings' => array(array('fieldName' => 'state.country'))
))
. , .
:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('name')
->add('state.name')
->add('state.country.name')
;
}
, .
, . , ?