我尝试将“考试类别”添加到我的表单中:
class ExamType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('categories', EntityType::class, [
'class' => ExamCategory::class,
'multiple' => true,
'expanded' => false,
'label' => 'Kategoria',
'choice_label' => 'name',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.name');
},
])
// ...
;
}
}
这是ExamCategory实体:
class ExamCategory
{
// ...
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Exam", mappedBy="categories")
*/
private $exams;
// ...
/**
* @return Collection|Exam[]
*/
public function getExams(): Collection
{
return $this->exams;
}
// ...
}
和基础考试实体:
class Exam
{
/**
* @ORM\ManyToMany(targetEntity="App\Entity\ExamCategory", inversedBy="exams")
*/
private $categories;
// ...
/**
* @return Collection|ExamCategory[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
// ...
}
我曾尝试从Doctrine清除缓存和元数据,但这无济于事。
删除此字段后,表单呈现就没有问题了(命名空间是正确的,我没有发现任何错字)。