我有一个由StrainGmoType继承的主窗体(StrainType),而StrainGmoType嵌入了一个StrainPlasmidType。
在主窗体(StrainType)中,我有一个用户选择团队的字段,然后一些字段取决于这个选择(在main,child和embded表单中)。对于在主窗体或子窗口中声明的所有字段,它都有效。但是我有2个CollectionType,在这个CollectionType中我有很多与参数的关系。然后我做了一个特定的Type(StrainPlasmidType),在main和child类型中,我在CollectionType中调用它们(在示例中只在子节点中))。但是,我无法获得此类型的团队字段,那么我不能做PRE_SET_DATA,PRE_SUBMIT或POST_SUBMIT ......
我尝试使用inherit_data,但它并不适合这种情况。如何获取StrainPlasmidType中团队字段的数据?
代码的一部分:
The StrainForm:
class StrainType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('team', EntityType::class, [
'class' => 'AppBundle\Entity\Team',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('team')
->leftJoin('team.members', 'members')
->leftJoin('team.projects', 'projects')
->leftJoin('projects.members', 'project_members')
->where('members = :user')
->orWhere('project_members = :user')
->setParameter('user', $this->tokenStorage->getToken()->getUser());
},
'data' => $this->tokenStorage->getToken()->getUser()->getFavoriteTeam(),
'choice_label' => 'name',
'placeholder' => '-- select a team --',
'mapped' => false,
])
GmoStrain表格:
class StrainGmoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('strainPlasmids', CollectionType::class, [
'entry_type' => StrainPlasmidType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
])
//...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\Strain',
]);
}
public function getParent()
{
return StrainType::class;
}
}
StrainPlasmidType:
class StrainPlasmidType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('plasmid', EntityType::class, [
'class' => 'AppBundle\Entity\Plasmid',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->leftJoin('p.team', 'team')
->leftJoin('team.members', 'members')
->where('members = :user')
->setParameter('user', $this->tokenStorage->getToken()->getUser())
->orderBy('p.autoName', 'ASC')
;
},
'group_by' => function (Plasmid $plasmid) {
return $plasmid->getTeam()->getName();
},
'choice_label' => function (Plasmid $plasmid) {
return $plasmid->getAutoName().' - '.$plasmid->getName();
},
'placeholder' => '-- select a plasmid --',
])
->add('state', ChoiceType::class, [
'choices' => [
'Replicative' => 'replicative',
'Integrated' => 'integrated',
'Cured' => 'cured',
],
'placeholder' => '-- select a state --',
])
;
答案 0 :(得分:0)
最后,我只使用表单选项将数据发送到嵌入表单:
主要:
$form->add('strainPlasmids', CollectionType::class, [
'entry_type' => StrainPlasmidType::class,
'entry_options' => [
'parent_data' => $team,
],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
]);
并且,以嵌入形式:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('plasmid', EntityType::class, [
'class' => 'AppBundle\Entity\Plasmid',
'query_builder' => function (EntityRepository $er) use ($options) {
return $er->createQueryBuilder('p')
->leftJoin('p.team', 'team')
->where('team = :team')
->setParameter('team', $options['parent_data'])
->orderBy('p.autoName', 'ASC')
;
},
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\StrainPlasmid',
]);
$resolver->setRequired(array('parent_data'));
}