I'd like to provide a direct link to related entities of my current entity-listing.
I've tried to use addIdentifier
for the related entities column but it redirects me to my current entity.
I found nothing in the doc explaining how to achieve this.
Here is an example of what I'm doing:
I'm currently listing my "data profile" entities. They have related entities such as "Entrainement", "Niveau", or "Programme". I would like to be redirected to the "Programme" list if I click on "Se defouler", and similarly for each of the columns which display related entities.
Here is my configureListFields
implementation from my current admin class:
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier( 'type.titre' , null, array ( 'label' => 'Type') )
->add('valeur', null, array ( 'label' => 'Valeur' ) )
->add('bioprofile.titre', null, array ( 'label' => 'Bio Profile' ) )
->add( 'exercicesData.entrainement.niveau.titre', null, array( 'label' => 'Niveau' , 'route' => array( 'name' => 'edit') ) )
->add( 'exercicesData.entrainement.titre' , many_to_one, array ( 'label' => 'Entrainement' ) )
->add( 'exercicesData.entrainement.niveau.programme.titre' , null , array ( 'label' => 'Programme' ) )
->add('_action', 'actions', array(
'actions' => array(
'show' => array(),
'edit' => array(),
'delete' => array(),
)
))
;
}
I tried different ways:
Neither of them works.
PS: In the screenshot, there are some links on "Niveau 2", "Seance 1" and "se defouler". They were actually displayed using addIdentifier
.
Sonata add a link to the related entity only when you add the entity, and not when you add a property of the entity.
The following example will add a link to Entity2
:
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
...
->add('Entity2')
...
;
}
Whereas this example will not add a link to the entity2
:
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
...
->add('Entity2.name')
...
;
}
If you want to display the name
property in the link, you have to add or change the __toString()
method in Entity2
:
class Entity2
{
...
public function __toString()
{
return (string) $this->name;
}
}
Your admin class should be like this:
/**
* @param ListMapper $listMapper
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier( 'type.titre' , null, array ( 'label' => 'Type') )
->add('valeur', null, array ( 'label' => 'Valeur' ) )
->add('bioprofile.titre', null, array ( 'label' => 'Bio Profile' ) )
->add( 'exercicesData.entrainement.niveau', null, array( 'label' => 'Niveau' , 'route' => array( 'name' => 'edit') ) )
->add( 'exercicesData.entrainement' , many_to_one, array ( 'label' => 'Entrainement' ) )
->add( 'exercicesData.entrainement.niveau.programme' , null , array ( 'label' => 'Programme' ) )
->add('_action', 'actions', array(
'actions' => array(
'show' => array(),
'edit' => array(),
'delete' => array(),
)
))
;
}
And you should change the following entities:
Entity Entrainement
:
class Entrainement
{
...
public function __toString()
{
return (string) $this->titre;
}
}
Entity Niveau
:
class Niveau
{
...
public function __toString()
{
return (string) $this->titre;
}
}
Entity Programme
:
class Programme
{
...
public function __toString()
{
return (string) $this->titre;
}
}
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With