0
我想將我的部門的'名稱'顯示爲我的用戶的下拉列表,當他們選擇一個時,將其'id'保存到數據庫中。我怎樣才能做到這一點?以下是我的代碼。 Currentlt顯示ID和保存ID。在symfony下拉列表中顯示名稱並保存ID
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('adress')
->add('city')
->add('area')
->add('departments', 'entity', array(
'empty_value' => '',
'class' => 'YdyaHospitalBundle:Department',
'query_builder' => function($repository) { return $repository->createQueryBuilder('t')->orderBy('t.id', 'ASC'); },
'property' => 'id',
'multiple' =>false,
'expanded' =>true,
))
;
}
更新
我的控制器動作:
/**
* Creates a new Hospitals entity.
*
* @Route("/", name="hospitals_create")
* @Method("POST")
* @Template("YdyaHospitalBundle:Hospitals:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Hospitals();
$form = $this->createForm(new HospitalsType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('hospitals_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Displays a form to create a new Hospitals entity.
*
* @Route("/new", name="hospitals_new")
* @Method("GET")
* @Template()
*/
public function newAction()
{
$entity = new Hospitals();
$form = $this->createForm(new HospitalsType(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
ThankYou.Can請您更清楚地解釋它的一些?將屬性設置爲名稱會引發錯誤bcoz數據庫需要一個整數。 –
你能否在你處理提交的表單的地方發佈代碼並將數據插入你的數據庫? – Syjin
@ Syjin ..我對symfony很陌生,我仍然無法理解它的工作流程。不過,我已經發布了代碼。請參閱更新後的問題 –