Symfony:必须管理传递给选择字段的“App\Entity\Classement”类型的实体。也许您忘记将其保留在实体管理器中?
Posted
技术标签:
【中文标题】Symfony:必须管理传递给选择字段的“App\\Entity\\Classement”类型的实体。也许您忘记将其保留在实体管理器中?【英文标题】:Symfony : Entity of type "App\Entity\Classement" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?Symfony:必须管理传递给选择字段的“App\Entity\Classement”类型的实体。也许您忘记将其保留在实体管理器中? 【发布时间】:2021-08-14 22:35:32 【问题描述】:我有这个表格:
public function buildForm(FormBuilderInterface $builder, array $options)
$builder
->add('country', EntityType::class, [
'class' => Country::class,
'choice_label' => 'name',
'label' => false,
'placeholder' => '-',
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('c')
->orderBy('c.name', 'ASC');
])
->add('season', EntityType::class, [
'class' => Season::class,
'choice_label' => 'year',
'label' => false,
'placeholder' => '-',
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('s')
->orderBy('s.year', 'ASC');
])
->add('clubHistos', CollectionType::class, [
'entry_type' => ClubHistoType::class,
'entry_options' => [
'label' => false
],
'by_reference' => false,
'allow_add' => true
])
;
public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults([
'data_class' => Classement::class,
]);
这是我的 ClubHistoType :
public function buildForm(FormBuilderInterface $builder, array $options)
$builder
->add('position', null, [
'label' => false
])
->add('matches', null, [
'label' => false
])
->add('victories', null, [
'label' => false
])
->add('draws', null, [
'label' => false
])
->add('losses', null, [
'label' => false
])
->add('goals', null, [
'label' => false
])
->add('points', null, [
'label' => false
])
->add('season', EntityType::class, [
'class' => Season::class,
'choice_label' => 'year',
'label' => false,
'placeholder' => '-',
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('s')
->orderBy('s.year', 'ASC');
])
->add('club', EntityType::class, [
'class' => Club::class,
'choice_label' => 'name',
'label' => false,
'required' => false,
'placeholder' => '-',
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('c')
->orderBy('c.name', 'ASC');
])
;
public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults([
'data_class' => ClubHisto::class,
]);
还有我在控制器中创建新国家/地区表的功能:
/**
* @Route("back/table/new", name="new_table")
*/
public function createCountryTable(Request $request, EntityManagerInterface $manager)
$countryTable = new Classement();
$clubHistoriques = new ClubHisto();
$countryTable->addClubHisto($clubHistoriques);
$form = $this->createForm(ClassementType::class, $countryTable);
dd($form);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
// foreach($clubHistoriques as $histo)
// $histo->setSeason($countryTable->getSeason());
//
$manager->persist($countryTable);
$manager->flush();
return $this->redirectToRoute('edit_table', ['id' => $countryTable->getId()]);
return $this->render('back/createTables.html.twig', [
'form' => $form->createView()
]);
但是一旦我调用表单,它就会给我这个错误:
必须管理传递给选择字段的“App\Entity\Classement”类型的实体。也许您忘记将其持久化在实体管理器中?
我不太明白,因为我还有其他形式与这个完全一样,而且一切正常......
我知道这个问题已经被问过很多次了,但是给出的解决方案从来没有奏效,但从来没有真正奏效过。
非常感谢您的帮助!
【问题讨论】:
【参考方案1】:尝试在没有$countryTable
的情况下实例化您的表单
$form = $this->createForm(ClassementType::class);
您已经将表单绑定到数据,因此您不需要预先实例化它。
然后处理您的if (form->isSubmitted())
逻辑中的数据
/* @var $countryTable \App\Entity\Classement */
$countryTable = $form->getData();
$clubHistos = $countryTable->getClubHistos();
【讨论】:
以上是关于Symfony:必须管理传递给选择字段的“App\Entity\Classement”类型的实体。也许您忘记将其保留在实体管理器中?的主要内容,如果未能解决你的问题,请参考以下文章
Symfony2:传递给 Doctrine\ORM\EntityRepository::__construct() 的参数 2 必须是 Doctrine\ORM\Mapping\ClassMetada
Symfony-Catchable致命错误:传递给Doctrine Common Collections ArrayCollection :: __ construct()的参数1必须是类(代码