1
我想用自定義查詢生成器創建使用Symfony 2.7的依賴窗體,但是PRE_SUBMIT不斷實例化錯誤的對象。Symfony 2.7中的依賴窗體錯誤:非對象上的getId()
我上傳了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);
});
}
Error: Call to a member function getId() on a non-object
好吧我改變了我的代碼並更新了相同的問題,但現在它說**期望的參數類型「Doctrine \ ORM \ QueryBuilder」,「數組」給出**雖然我正在使用querybuilder對象 –
這是一個完全不同的問題,但你能給我一個錯誤發生的地方嗎?最好的辦法是在你的問題中添加完整的異常跟蹤。 – chalasr
我使用堆棧跟蹤更新了問題...並且它沒有向我顯示它在我的代碼中發生的行,它指向供應商文件夾中的EntityType。 –