【问题标题】:Symfony Forms Error: Entity of type "..." passed to the choice field must be managed. Maybe you forget to persist it in the entity managerSymfony 表单错误:必须管理传递给选择字段的“...”类型的实体。可能你忘记在实体管理器中持久化
【发布时间】:2020-11-09 16:55:08
【问题描述】:

目标

我正在尝试制作一个基本表格,让新球员参加一项运动。这取自 Symfony 例如在:

https://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data

代码

我有 3 个实体:

玩家列表 https://github.com/ChimeraBlack1/Symphart/blob/main/src/Entity/PlayerList.php

运动 https://github.com/ChimeraBlack1/Symphart/blob/main/src/Entity/Sport.php

位置 https://github.com/ChimeraBlack1/Symphart/blob/main/src/Entity/Position.php

我有一个表格:

新玩家类型 https://github.com/ChimeraBlack1/Symphart/blob/main/src/Form/NewPlayerType.php

我有一个控制器

NewPlayerController https://github.com/ChimeraBlack1/Symphart/blob/main/src/Controller/NewPlayerController.php

错误:

Entity of type "Doctrine\Common\Collections\ArrayCollection" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?

详情:

每当我使用类型为“EntityType”的表单构建器创建表单时,我似乎都会收到此错误,如下所示:

参考https://github.com/ChimeraBlack1/Symphart/blob/main/src/Form/NewPlayerType.php (第 22 行)

    ->add('sport', EntityType::class, [
        'class' => Sport::class,
        'query_builder' => function(EntityRepository $er) {
            return $er->createQueryBuilder('s')
                ->orderBy('s.sport', 'ASC');
        },
        'choice_label' => 'sport',
    ])

在我看来,这是因为我在“NewPlayerType”表单中引用“Sport::class”。如果我要引用“PlayerList::class”,我不会遇到错误。但是,我如何获取基于类似这样的其他实体的字段以填充到单个表单上?我认为我在这里遗漏了一些概念上的东西......

【问题讨论】:

    标签: php forms symfony dynamic dynamic-forms


    【解决方案1】:

    好的,经过 20 个小时的谷歌搜索、捂脸和愤怒的眼泪,我想通了。

    问题在于我在实体之间设置的关系。我有“OneToMany”关系,而我应该有“ManyToOne”。

    如果您发现此问题,请从“倒退”的角度重新编写您的关系,这应该可以解决问题。

    谢谢!

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      相关资源
      最近更新 更多