dongqian6234 2018-07-13 15:19
浏览 24
已采纳

为多个实体创建一个常规管理员,但在编辑时调用特定管理员

Lets assume i have a abstract doctrine entity class

abstract class BaseClass
{
    private $title;
    private $description;
}

and two inherited child classes

class A extends BaseClass
{
}

and with an extra Field

class B extends BaseClass
{
    private $extraField;
    ...
}

For those two entities i want to create one admin for listing both together in one view. So in my admin.yml i add

AppBundle\Admin\BaseClassAdmin:
      tags:
        - { name: sonata.admin, manager_type: orm, label: "base" }
      arguments:
        - null
        - AppBundle\Entity\BaseClass
        -
      calls:
        - [setSubClasses, [{'A' : 'AppBundle\Entity\A', 'B' : 'AppBundle\Entity\B'}]]

And the BaseClass Admin class is

class BaseClassAdmin extends AbstractAdmin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('title');
    }

    protected function configureFormFields(FormMapper $form)
    {
        $form->add('title');
        $form->add('description');
    }
}

What i want to do is that the listview displays both types which is not the problem. But when i click on one item which is of type B i want to show the extraField in my form. But it doesn't.

This is cause instead calling the particular admin for B (which has extraField added in form) its calling only the general admin (BaseClassAdmin).

So my question

Is there a way to call the particular admin corresponding to the type of the entity from the list view? Or is the only way to modify the BaseClassAdmin and solving it by adding ugly if-statements in the form method?

  • 写回答

2条回答 默认 最新

  • duan1979768678 2018-07-13 17:19
    关注

    You can call $this->getSubject() in configureFormFields, and add fields conditionally based on an instanceof check.

    https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/advanced_configuration.html#inherited-classes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作