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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序