【发布时间】: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