Sonata admin bundle, manipulate objects

前端 未结 2 1712
天命终不由人
天命终不由人 2021-01-26 17:55

I have 2 entities with one to many relationship project and prototype And I\'ve been looking for a way to list the prototypes that belong to a project in the show action . here

2条回答
  •  佛祖请我去吃肉
    2021-01-26 18:30

    One way is to define the template for your prototypes field in showMapper

    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper ->add('prototypes',null, array('template' => 'NamespaceYourBundle::Admin/prototypes.html.twig'));
    
    }
    

    Create Admin folder in your bundle's resources folder and create prototypes.html.twig file ,extend your twig template with sonata's base_show_field.html.twig template and in {% block field %} define your own markup looping through all related prototypes

    {% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
    {% block field %}
        {% spaceless %}
            {% if object.getPrototypes() is not empty %}
                
                    ...
                    ...
                    ...
                
                {% for prototype in object.getPrototypes() %}
                
                    ...
                    ...
                    ...
                
                {% endfor %}
                
    Nom Description
    {{ prototype.getNom() }} {{ prototype.getDescription() }}
    {% endif %} {% endspaceless %} {% endblock %}

提交回复
热议问题