Car*_*uez 2 php forms symfony doctrine-orm
在我的Symfony项目(2.7)中,我有一个Apartment具有很多属性的实体.其中之一就是Town.
Town是其他学说实体,他们有一个City实体,并City有一个State.
在我的Apartment形式,我有3个选择.为和Town,City和State.但是当我想要保存时,我只想要保存Town.
...
$builder->add('town', 'entity', array(
'label' => 'Town',
'choices' => $towns,
'class' => "AppBundle\Entity\Town"
));
$builder->add('city', 'entity', array(
'label' => 'City',
'choices' => $cities,
'class' => "AppBundle\Entity\City"
));
$builder->add('state', 'entity', array(
'label' => 'States',
'choices' => $states,
'class' => "AppBundle\Entity\State"
));
...
Run Code Online (Sandbox Code Playgroud)
有可能取消了我不想保存实体公寓的额外字段吗?
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
//I want to unset State and City entities.
$apartment = $form->getData();
...
}
Run Code Online (Sandbox Code Playgroud)
我有这个错误:
Neither the property "state" nor one of the methods "addState()"/"removeState()", "setState()", "state()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\Apartment".
Run Code Online (Sandbox Code Playgroud)
提交后,表单数据不能更改.但是您可以在提交完成之前附加事件监听器来执行此操作:
# Don't forget these
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
# ...
$builder->add('city', 'entity', array(
'label' => 'City',
'choices' => $cities,
'class' => "AppBundle\Entity\City",
'mapped' => FALSE // <-- This is important
));
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event){
$data = $event->getData();
$data['city'] = NULL;
$data['state'] = NULL;
# We need this because of PHP's copy on write mechanism.
$event->setData($data);
});
Run Code Online (Sandbox Code Playgroud)
如果你需要这些被NULL验证过程中,交换之前-ed POST_SUBMIT的SUBMIT.
现在,form->getData()在您的控制器内调用将返回NULL值.
希望这可以帮助...
| 归档时间: |
|
| 查看次数: |
1796 次 |
| 最近记录: |