我有三个实体。Profile
,Car
和Trip
。当user(Profile
)创建时Trip
,他可以选择Car
(仅由他自己选择)并将其分配给Trip
。我知道该字段必须是实体类型。但是我不知道如何设置选择列出当前用户(配置文件)的汽车。有任何想法吗?谢谢。
按用户筛选汽车,我认为这个例子是您需要的:
$builder->add('car', 'entity', array(
'class' => '/path/to/entity/Car',
'property' => 'title',
'empty_value' => 'Choose a car',
'query_builder' => function(EntityRepository $em) use ($userId) {
return $em->createQueryBuilder('c')
->join('c.user', 'u')
->where('u.id = :userId')
->setParameter('userId', $userId);
}
)
)
您可以将$ userId添加为一种表单选项:
$form = $this->createForm(new MyFormType(), $object, array( 'userId' => $userId ));
并在您的表单中检索它:
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'userId' => function (Options $options, $value) {
return $options['userId'];
}
));
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if($options['userId']){
$userId = $options['userId'];
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句