我创建了这样的自定义类型:
private $selectedCountryId = 0;
public function configureOptions(OptionsResolver $resolver)
{
$queryBuilder = function (CountryRepository $er) {
return $er->createQueryBuilder('c')
->where('c.active = 1')
->orWhere('c.id = :sid')
->orderBy('c.ord', 'ASC')
->addOrderBy('c.name', 'ASC')
->setParameter('sid', $this->selectedCountryId)
;
};
$resolver->setDefaults(array(
'query_builder' => $queryBuilder,
'class' => Country::class,
));
}
public function getParent()
{
return EntityType::class;
}
public function getBlockPrefix()
{
return 'CountryType';
}
我想要做的是根据实体的实例修改selectedCountryId参数:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(
FormEvents::PRE_SET_DATA, array($this, 'onPreSetData')
);
}
public function onPreSetData(FormEvent $event)
{
if ($event->getData() instanceof Country) {
$this->selectedCountryId = $event->getData()->getId();
}
}
但是setOptions在这个阶段已经被调用,我不知道如何在这里修改查询生成器。 通常,我要达到的目标是,如果已针对给定实体存储了该国家/地区,则允许该国家/地区在下拉菜单中使用。
答案 0 :(得分:1)
在那种情况下,您仍然有机会在PRE_SET_DATA
事件上修改查询构建器,因为在创建表单视图之前不会构建选择列表。
这应该可以解决问题:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
if (null !== $country = $event->getData()) {
// at this point the option is already resolved
// so it'll return the QueryBuilder instance
$qb = $event->getForm()->getConfig()->getOption('query_builder');
$qb->orWhere('c = :country')->setParameter('country', $country);
}
});
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class' => Country::class,
'query_builder' => function (CountryRepository $r) {
return $r->createQueryBuilder('c')
->where('c.active = 1')
->orderBy('c.ord', 'ASC')
->addOrderBy('c.name', 'ASC')
;
},
]);
}
仅适用于对象实例。