使用最新的 Symfony,我有一个自定义表单,其中根据原则请求在实体上创建一个字段。要构造此请求,需要id
控制器传递一个参数,并带有选项“postedBy”
class StudyDecomposedType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('subjects', EntityType::class,[
'class' => Subject::class,
'choice_label' => 'identifier',
'required' => false,
'multiple' => true,
'query_builder' => function (EntityRepository $er) use ($options): QueryBuilder {
return $er->createQueryBuilder('a')
->leftJoin("a.studies", "s")
->leftJoin(Studyaccess::class, 'sa', 'WITH', 's=sa.study')
->where('(sa.user = :userid AND (sa.r = 1 OR sa.w = 1)) OR (a.owner= :userid)')
->setParameter('userid', $options['postedBy']);
},
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'postedBy' => 0,
]);
$resolver->setAllowedTypes('postedBy', 'int');
}
在我的控制器中,我制作如下形式:
$form = $this->createForm(StudyDecomposedType::class,
$options= ['postedBy' => $this->getUser()->getId(),]
);
但无论我在选项中输入什么值,请求中的值都是我在默认值中输入的值(此处为 0)。
有人可以帮助我吗,我完全迷失了
该参数userid
应该是通过调用$options
crontroller 给出的参数,但该值永远不会改变。我验证了控制器中的 id,我查看了生成的 sql,并且userid
始终为其默认值