终于成功了!需要像上一个答案一样使用迁移,这里是 FormType 的代码
class CustomersOptionSelectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (PreSetDataEvent $event) {
$form = $event->getForm();
$lineItem = $event->getForm()->getParent()->getData();
$customer = $lineItem->getCustomerUser()->getCustomer();
$qb = $event->getForm()->getConfig()->getOption('query_builder');
$qb->setParameter('customer', $customer->getId());
});
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'class' => Option::class,
'choice_label' => 'name',
'required' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('o')
->where('o.customer = :customer')
->orderBy('o.name', 'ASC')
;
},
]);
}
/**
* {@inheritdoc}
*/
public function getParent(): string
{
return Type\Select2EntityType::class;
}
}