我在Symfony2中工作,我有一個窗體,其中顯示了數據庫中出現的複選框。我想將所選的複選框保存在數據庫中(mayby可以像數組或字符串,如value1,value2)。我該如何處理這如何使用複選框保存和編輯多個值
這是我的代碼:
實體:
/**
* @var string
*/
private $relevantewaben;
/**
* @return string
*/
public function getRelevantewaben()
{
return $this->relevantewaben;
}
/**
* @param string $relevantewaben
*/
public function setRelevantewaben($relevantewaben)
{
$this->relevantewaben = $relevantewaben;
}
控制器:
public function newAction()
{
$entity = new ChangeRequest();
$form = $this->createCreateForm($entity);
return $this->render('OtlgCmdbBundle:ChangeRequest:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('OtlgCmdbBundle:ChangeRequest')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find ChangeRequest entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('changerequest_edit', array('id' => $id)));
}
return $this->render('OtlgCmdbBundle:ChangeRequest:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
形式:
->add('relevantewaben', 'entity', array(
'class' => 'OtlgCmdbBundle:applicant',
'label' => 'Benötigte, relevante Waben zur Umsetzung',
'property' => 'name',
'expanded' => true,
'required' => false,
'multiple' => true,
'attr' => array('class' => 'css-checkbox')
謝謝你對我而言,最好的解決方法是建立表格之間的關係。 – kornjan