无法读取由 $form->createView() 触发的类"AppEntityTravel" "title"



我的symfony页面上出现以下错误。当我尝试编辑一个项目时,使用CRUD系统,我会遇到以下错误:

无法获得读取属性的方法"标题";在课堂上"应用程序\实体\旅行"。

我的"旅行;实体没有这样的";标题";属性,因为它不是预期的。唯一的地方是";标题";属性定义在TravelTranslation实体中,该实体与travel之间存在ManyToOne关系。

在我评论了我的小树枝模板中对表单的所有引用后,我发现错误是由我的控制器中的$form->createView()触发的。

/**
* @Route("/{id}/edit", name="travel_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Travel $travel): Response
{
$entityManager = $this->getDoctrine()->getManager();
$langs = $entityManager->getRepository(Lang::class)->findAll();
$media = $entityManager->getRepository(Media::class)->findAll();
$form = $this->createForm(TravelType::class, $travel);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('travel_index');
}
return $this->render('crud/travel/edit.html.twig', [
'langs' => $langs,
'travel' => $travel,
'media' => $media,
'form' => $form->createView()
]);
}

但我的TravelType表格包含以下代码:

class TravelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('main_title')
->add('category', EntityType::class,[
'class' => Category::class,
'choice_label' => 'name',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('c')
->andWhere('c.type = :type')
->setParameter('type', 'country')
->orderBy('c.name', 'ASC');
},
])
->add('price_driver', MoneyType::class,[
'divisor' => 100,
])
->add('price_codriver', MoneyType::class,[
'divisor' => 100,
])
/*  ->add('country') */
->add('km')
->add('media', EntityType::class, [
'class' => Media::class,
'choice_label' => 'name',
'multiple' => true
])
->add('status')
->add('duration')
->add('level')
->add('travelTranslations', CollectionType::class, [
'entry_type' => TravelTranslationType::class,
'entry_options' => [
'label' => false
],
'by_reference' => false,
// this allows the creation of new forms and the prototype too
'allow_add' => true,
// self explanatory, this one allows the form to be removed
'allow_delete' => true
])
->add('dates', CollectionType::class, [
'entry_type' => DatesType::class,
'entry_options' => [
'label' => false
],
'by_reference' => false,
// this allows the creation of new forms and the prototype too
'allow_add' => true,
// self explanatory, this one allows the form to be removed
'allow_delete' => true
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Travel::class,
'allow_extra_fields' => true
]);
}
}

我设法修复了这个错误,方法是进入DatesType::class(一个自定义实体类型表单,而不是DateType(并修复它,因为它遇到了引用错误属性的麻烦"标题";在收集型中

通过更改

->add('travel', EntityType::class, [
'class' => Travel::class,
'choice_label' => 'title'
] )

签字人:

->add('travel', EntityType::class, [
'class' => Travel::class,
'choice_label' => 'main_title'
] )

其中main_title是不动产。

最新更新