Search code examples
phpsymfonysonata-admin

Show One To Many relationship in sonata admin


I want to show the one-to-many relationshop entity in the sonata admin's show action. I found answer to my problem at ("Sonata admin bundle, manipulate objects"). I try to implement @M Khalid Junaid's solution but I get an error "An exception has been thrown during the rendering of a template ("Warning: nl2br() expects parameter 1 to be string, object given") in SonataAdminBundle:CRUD:base_show_field.html.twig at line 13."

Did anyone here face this problem before?

GroupParticipant.php

class GroupRepresentive {
    ...
    /**
     * @ORM\OneToMany(targetEntity="GroupParticipant", mappedBy="representive", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    public $participant;

    public function __construct() {
        $this->participant = new ArrayCollection();
    }
    ...}

GroupRepresentativeAdmin.php

protected function configureShowFields(ShowMapper $showMapper)
        {
            $showMapper
                ->add('name')
                ->add('eventTitle')
                ->add('email')
                ->add('person')
                ->add('payment.paymentType')
                ->add('payment.bank')
                ->add('payment.userAccountNumber')
                ->add('payment.referenceNumber')
                ->add('payment.paymentAt')
                ->end()
                ->with('Participant')
                ->add('participant', 'null', array(
                    'template' => 'AppBundle::Admin/groupParticipant.html.twig'
                ))
            ;

        }

groupParticipant.html.twig

{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
{% block field %}
    {% spaceless %}

    {% endspaceless %}
{% endblock %}

I put the custom template at

enter image description here


Solution

  • Because you didn't really extend the

    SonataAdminBundle:CRUD:base_show_field.html.twig

    try this

        {% block field %}
             {# show a field of your entity for example the name #}
             {{value.name}}
        {% endblock %}