I had a 'User' entity which contains one 'Role' entity.
Controller code:
/**
* @Route("/root/users/edit/{id}", requirements={"id" = "\d+"})
* @Method({"GET", "POST"})
*/
public function usersEditAction(Request $request, User $user)
{
$form = $this->createFormBuilder($user)
->add('username', 'text')
->add('role', 'entity', array( 'class' => 'Lan\CsmBundle\Entity\Role', 'property' => 'role'))
->add('id', 'hidden')
->getForm();
if ($request->isMethod('POST'))
{
$form->submit($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirect($this->generateUrl('root_users'));
}
}
return $this->render('CsmBundle:Root:users_edit.html.twig', array(
'form' => $form->createView()
));
}
This code edits 'User' object with one role and 'Role' object was displayed as 'select' on HTML form.
Now, i have changed scheme of db and 'User' may have many 'Role's. On form it can be displayed as 'select' with multiple attribute. But i don't know a true-way how to do that on symfony2.