简体   繁体   English

在奏鸣曲管理员中显示一对多关系

[英]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 "). 我在(“ Sonata管理员捆绑包,操作对象 ”)中找到了解决问题的方法。 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. " 我尝试实现@M Khalid Junaid的解决方案,但遇到错误“ SonataAdminBundle:CRUD:base_show_field中的模板渲染过程中引发了异常(“警告:nl2br()期望参数1为字符串,给定对象”) .html.twig在第13行。

Did anyone here face this problem before? 这里有人遇到过这个问题吗?

GroupParticipant.php 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 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 groupParticipant.html.twig

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

    {% endspaceless %}
{% endblock %}

I put the custom template at 我将自定义模板放在

在此处输入图片说明

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 %}

Though the previous answer have been accepted, I'm providing solution for users who are using Symfony 4 and Symfony 3.4 (Symfony Flex). 尽管先前的答案已被接受,但是我正在为使用Symfony 4和Symfony 3.4(Symfony Flex)的用户提供解决方案。

Field in Admin class must be like: Admin类中的字段必须类似于:

GroupRepresentativeAdmin.php GroupRepresentativeAdmin.php

->add('participant', 'null', array(
                    'template' => 'folderName/fileName.html.twig'
                ));

Note that the folder must be in your templates directory. 请注意,该文件夹必须位于您的模板目录中。 The path to your twig file must be templates/folderName/fileName.html.twig 树枝文件的路径必须是template / folderName / fileName.html.twig

The content in twig file must be like: 树枝文件中的内容必须类似于:

fileName.html.twig fileName.html.twig

{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}

{% block field %}
    {% spaceless %}
           //Your custom operation
    {% endspaceless %}
{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM