我和实体A将oneToMany与实体B联系起来.我希望用户可以选择从现有的B实体中选择或者在A类的形式上创建一个新的实体.到目前为止,我在我的表单上有这个:
->add('ExistingB', 'entity', array(
'class' => 'AppBundle\Entity\B',
'required' => false,
'property_path' => 'B',
'mapped' => false,
))
->add('newB', new BType(), array(
'required' => false,
'property_path' => 'B',
'mapped' => false,
));
$builder->addEventListener(
FormEvents::POST_SUBMIT , function (FormEvent $event) {
$formB= $event->getForm()->get('newB');
if ($formB->get('name')->getData() != null){
//here i need to somehow say to the form that it needs to set mapped true
//to the formB field so it can create a new entity and update the relationship
}else{
//here I need to do the same but with the ExistingB field
}
}
);`
Run Code Online (Sandbox Code Playgroud)
我无法找到如何更改映射的属性,以及我得到它的次数,它确实创建了实体.我想这是因为在post_submit事件中由于数据已经下载到A实体而对字段进行更改太晚了.Buuut ..如果我使用pre_submit事件,那么我无法获取子formB的数据,因为当我要求它时它总是给我null.
那么......我的大错是哪里?有人可以告诉我另一种方法来处理synfony2表单中的新功能或现有功能.我真的不敢相信,实施如此普遍的行为可能很难.
非常感谢提前
| 归档时间: |
|
| 查看次数: |
1281 次 |
| 最近记录: |