reptilex
reptilex

Reputation: 131

how to embed one to many sonata admin child views

I know you can add fields of a child view in sonata admin by point referencing child.field in the show view, but that only works for one to one relationships. I have a one parent - many - child relationship. And this does not work:

<?php $showMapper->add('child', 'sonata_type_model',array(), array(
                'edit' => 'inline',
                'inline' => 'table',
                ))

Although it works great for the formMapper. How are you supposed to do this? If it is through the twig view please give me an example.

Upvotes: 0

Views: 1136

Answers (1)

Mawcel
Mawcel

Reputation: 2007

For the ShowMapper you just do $showMapper->add('child');

However it will most of the time only display a link to the child object.

As you suggested you can also directly render child fields with

$formMapper->add('child.field');

But it will not work for collections

To customize the rendering when having a collection you can make your own template extending SonataAdminBundle:CRUD:base_show_field.html.twig

In the template your child object(s) will be in the value variable wich you can loop on to display each element

Then add your field specifying the template

$showMapper->add('children', null, array('template' =>'MyBundle:CRUD:MyCustomTemplate.html.twig'));

Upvotes: 1

Related Questions