简体   繁体   English

如何添加链接以在SonataAdminBundle中的关系字段上显示关系实体的动作

[英]How to add link to show action of the relation entity on relation field in SonataAdminBundle

Im making admin panel in SonataAdminBundle. 我在SonataAdminBundle中制作管理面板。 In User show action i have field companies which return array of companies assigned to the user. 在“用户显示操作”中,我有现场公司,该公司返回分配给用户的公司阵列。 It is a OneToMany relation. 这是一个一对多的关系。 UserCompany has user_id and company_id. UserCompany具有user_id和company_id。 I want to create link on each returned company name, which points to it's entity show action. 我想在每个返回的公司名称上创建链接,该链接指向它的实体展示动作。 This is code from configureShowFields() function in UserAdmin class: 这是来自UserAdmin类中的configureShowFields()函数的代码:

                ->with('Assigned organizers',['class' => 'col-md-6'])
                    ->add('companies', null, [
                        'label' => 'Organizers',
                    ])
                ->end()

I managed to create a link on a string field pointing to show action of an entity, but the id property is taken from the current entity view: 我设法在一个字符串字段上创建一个链接,以指向显示实体的动作,但是id属性是从当前实体视图中获取的:

            ->with('Address', ['class' => 'col-md-6'])
                ->add('userProfile.locality', 'url', [
                    'route' => [
                        'name' => 'admin_app_employee_show',
                        'identifier_parameter_name' => 'id'
                    ],
                    'label' => 'Localiy',
                ])

What's more Sonata Admin create links on related fields, when the relation is direct, for example: Company has many Employee. 此外,当关系为直接关系时,例如,Sonata Admin在相关字段上创建链接:公司有很多员工。 Then in Company show action on employees field I see array with links already heading to edit action of Employee entty. 然后在“公司对员工的操作显示”字段中,我看到带有链接的数组,这些链接已经朝向编辑“员工实体”的操作。

Maybe there is a possibility to override template for this field, but it seems unclear for me, as the documentation lacks of more advanced examples. 也许可以覆盖该字段的模板,但由于文档缺少更高级的示例,因此对我来说似乎还不清楚。 This is how I tried to test overriding the template of a field: 这就是我尝试测试覆盖字段模板的方式:

                ->add('userProfile.street', null, array(
                    'label' => 'Street',
                    'template' => 'custom-field.html.twig',
                ))

Location of the template: App/templates/Admin/ 模板的位置:App / templates / Admin /

Any help appreciated 任何帮助表示赞赏

SonataAdmin automatically creates links to related entities once it has all of them configured and added to services. 一旦配置了所有相关实体并将其添加到服务,SonataAdmin就会自动创建到相关实体的链接。 Then you can just change the route action of the link on the relation field as following: 然后,您可以如下更改链接在关联字段上的路由操作:

            ->with('Assigned events', ['class' => 'col-md-6'])
                ->add('events', null, [
                    'route' => [
                        'name' => 'show'
                    ],
                    'label' => 'Events',
                ])
            ->end()

You can also change the type of relation field eg.'many_to_one' instead of null which might help in some cases. 您还可以更改关系字段的类型,例如'many_to_one'而不是null ,这在某些情况下可能会有所帮助。

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

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