【问题标题】:sonata Admin current entity奏鸣曲 Admin 当前实体
【发布时间】:2020-10-20 19:51:02
【问题描述】:

如何访问奏鸣曲管理员实体的 ID。假设我有一个实体 EndPoint,它有一个函数 getProject(),我怎样才能得到那个项目的 id。我尝试了下面的代码,但这给了我以下错误:尝试调用类“ContainerAgbGotv\srcApp_KernelDevDebugContainer”的名为“getProject”的未定义方法。

class EndPointAdmin extends AbstractAdmin{
protected function configureFormFields(FormMapper $form)
{   $container = $this->getConfigurationPool()->getContainer();
    $em = $container->getProject();
    
    $array = [];
    foreach ($em as $ems) {
        if (!empty($ems->getEnv())) {
            $array[$ems->getEnv()] = $ems->getEnv();
        }}
    $result = array_unique($array);
    $form
        
        ->add('name',ChoiceType::class, [
            'choices'=> $result,
            'placeholder' => 'Choose an option',

            'required' => false
        ])
        ->add('ipAdress',TextType::class)
        ->add('port',IntegerType::class)
               ;
}

感谢您的帮助。

【问题讨论】:

    标签: symfony sonata-admin sonata symfony-sonata sonata-user-bundle


    【解决方案1】:

    在管理员中,您将有权访问

    $this->getSubject()

    这将为您返回该管理员的当前实体。值得注意的是 configureFormFields 用于创建和编辑实体,因此 getSubject() 可能返回 null。

    【讨论】:

    • 感谢您的回复,但有时当我这样做时:$em =$this->getSubject()->getProject()->getEnvironments();我收到以下错误:在 null 上调用成员函数 getProject()
    • 因此在调用 getProject() 之前,您需要对 $this->getSubject()->getId() 进行空值检查。由于 configureFormFields 用于编辑和创建实体,例如创建时 getSubject() 将为空
    • 当我提交表单时,我收到以下错误“此表单不应包含额外的字段。”我无法修复它
    猜你喜欢
    • 2013-12-04
    • 2014-04-01
    • 2017-04-13
    • 1970-01-01
    • 2019-07-04
    • 2014-06-04
    • 1970-01-01
    • 2018-07-05
    • 2018-10-30
    相关资源
    最近更新 更多