Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Select 如何在表单上显示和编辑用户实体[symfony2]的角色集合?_Select_Symfony_Collections_Roles - Fatal编程技术网

Select 如何在表单上显示和编辑用户实体[symfony2]的角色集合?

Select 如何在表单上显示和编辑用户实体[symfony2]的角色集合?,select,symfony,collections,roles,Select,Symfony,Collections,Roles,我有一个“用户”实体,其中包含一个“角色”实体 控制器代码: /** * @Route("/root/users/edit/{id}", requirements={"id" = "\d+"}) * @Method({"GET", "POST"}) */ public function usersEditAction(Request $request, User $user) { $form = $this->createF

我有一个“用户”实体,其中包含一个“角色”实体

控制器代码:

/**
     *  @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()
            ));
    }
此代码使用一个角色编辑“用户”对象,“角色”对象在HTML表单上显示为“选择”


现在,我已经改变了数据库的方案,“用户”可能有许多“角色”。在表单上,它可以显示为具有多个属性的“选择”。但我不知道如何在symfony2上实现这一点。

正如您所看到的,它有一个属性。因此,您可以添加它。

不起作用,错误:应为条令\Common\Collections\Collection对象。请将
角色定义为实体中的ArrayCollection。像这里