Symfony在EntityType中使用错误的实体类

问题描述 投票:0回答:1

Symfony版本:4.4.5

我的表单类型为QuestionType,其中具有inputType字段-与实体inputType(manyToOne)的关系。我已经将class字段类型的EntityType参数设置为实体InputType

<?php

namespace App\Form;

use App\Entity\InputType;
use App\Entity\Question;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class QuestionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('question', TextType::class, [
                'required' => true,
                'label' => 'Frage',
            ])
            ->add('multipleAnswers', CheckboxType::class, [
                'required' => false,
                'label' => 'Mehrere Antworten zulassen',
            ])
            ->add('inputType', EntityType::class, [
                'class' => InputType::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('i')
                        ->orderBy('i.name', 'ASC');
                },
                'choice_label' => 'name',
            ])
            //->add('answers')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Question::class,
        ]);
    }
}

通过在浏览器中打开表单,出现以下错误:

[Semantical Error] line 0, col 45 near 'name ASC': Error: Class App\Entity\Answer has no field or association named name

我不明白为什么-表单的inputType字段已正确配置...

php symfony doctrine-orm symfony-forms
1个回答
-1
投票

请确保“实体答案”具有名为“名称”的字段,否则它将不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.