我想使用Symfony 2.7和自定义查询构建器创建依赖表单,但PRE_SUBMIT会继续实例化错误的对象。
我已将github中的整个代码上传了一些数据sampleData.sql
。
CityType
public function buildForm(FormBuilderInterface $builder,array$options){
$builder->add('country', 'entity', array(
'class' => 'TestMyBundle:Country',
'choice_label' => 'country'
));
$builder->add('Search','submit');
$stateFormbuilder = function(FormInterface $form,$country_Id){
$search = $form->get('Search');
$form->remove('Search');
$form->add('state','entity',array(
'class'=>'TestMyBundle:State',
'query_builder'=>function(EntityRepository $repository) use ($country_Id){
return $repository->createQueryBuilder('State')
->where('State.countryId= :countryId')
->setParameter('countryId',$country_Id)
->orderBy('State.state','ASC')
;
}
));
};
$CityFormbuilder = function(FormInterface $form, $state_Id){
$form->add('city','entity',array(
'class'=>'TestMyBundle:City',
'query_builder'=>function(EntityRepository $repository) use ($state_Id){
return $repository->createQueryBuilder('City')
->where('City.countryId= :countryId')
->setParameter('City.stateId',$state_Id)
->orderBy('City.city','ASC')
;
}
));
};
$CityAreaFormBuilder = function(FormInterface $form, $city_Id){
$form->add('cityArea','entity',array(
'class'=>'TestMyBundle:CityArea',
'query_builder'=>function(EntityRepository $repository) use ($city_Id){
return $repository->createQueryBuilder('CityArea')
->where('CityArea.city= :cityId')
->setParameter('CityArea.cityId',$city_Id)
->orderBy('CityArea.cityArea','ASC')
;
}
));
$form->add('Search','submit');
};
//$CityFormbuilder, $CityAreaFormBuilder
//$builder->add('Search', 'submit');
// State
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use($stateFormbuilder) {
$country = $event->getData();
$country_id = null;
if(null!== $country){
$country_id = $country->getId();
}
$stateFormbuilder($event->getForm(),$country_id);
});
$builder->addEventListener(FormEvents::PRE_SUBMIT,function(FormEvent $event) use($stateFormbuilder){
$data = $event->getData();
$country_id = array_key_exists('id',$data) ? $data['id']:null;
$stateFormbuilder($event->getForm(),$country_id);
});
// City
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use($CityFormbuilder) {
$state = $event->getData();
$stateId = null;
if(null!==$state){
$stateId = $state->getId();
}
$CityFormbuilder($event->getForm(),$stateId);
});
$builder->addEventListener(FormEvents::PRE_SUBMIT,function(FormEvent $event) use($CityFormbuilder){
$data = $event->getData();
$city_id = array_key_exists('id',$data) ? $data['id']:null;
$CityFormbuilder($event->getForm(),$city_id);
});
// City Area
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use($CityAreaFormBuilder) {
$cityArea = $event->getData();
$cityAreaId = null;
if(null!==$cityArea){
$cityAreaId= $cityArea->getId();
}
$CityAreaFormBuilder($event->getForm(),$cityAreaId);
});
$builder->addEventListener(FormEvents::PRE_SUBMIT,function(FormEvent $event) use($CityAreaFormBuilder){
$data = $event->getData();
$cityArea_Id = array_key_exists('id',$data) ? $data['id']:null;
$CityAreaFormBuilder($event->getForm(),$cityArea_Id);
});
}
错误:在非对象
上调用成员函数getId()
答案 0 :(得分:0)
如documentation中所述,如果没有数据传递给表单,$event->getData()
为空。
对于使用EventListener
事件的3个PRE_SET_DATA
中的每一个,
添加以下检查(我以最后一个事件为例):
$state = $event->getData();
$stateId = null;
if (null !== $state) {
$stateId = $state->getId();
}
然后,根据$stateId
的值来做你的逻辑。