如果您可以确定,用户每次注册都可以执行以下操作:
$builder->add('organizations', 'entity', array('property_path' => 'enrollment.organizations', ));
或者其他解决方案可能是,如果您定义一个查询构建器
$builder->add('organisations', 'entity', array(
'class' => '%ENTITY_NAME%',
'query_builder' => function (%REPOSITORY_NAME% $repository) {
return %QUERY%;
}
));
或使用事件监听器
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($factory) {
$data = $event->getData();
if ($data->getEnrollment()) {
$organisations = $data->getEnrollment()->getOrganisations();
} else {
$organisations = ...
}
$form = $event->getForm();
$form->add($factory->createNamed('organizations', 'entity', $organisations, array('required' => false)));
});