创建并处理表单后,如何设置Symfony EntityType表单字段的选择?

我正在与Symfony 3.4合作,并且想创建一个简单的表单,该表单显示一个列表实体,可以使用复选框选择删除这些实体:

  • 创建和处理表单的控制器从数据库中加载一系列实体,然后将其添加到表单中。
  • 控制器使用一些其他参数来提供分页,例如如果数据库中有10.000个实体,则用户可以选择仅加载前50个实体。

问题在于,“前50个实体”取决于是否删除了实体/表单是否已处理。

  • 要在控制器中处理表格,我必须先创建表格
  • 要创建表单,我需要从数据库中加载实体列表以将其设置为choises
  • 此列表将包含前50个实体
  • 如果用户选择删除前10个实体,则在处理表单时会这样做。

class SomeController {
    public function deleteUsersaction(Request request) {
        $users = $this->loadUsersFromDB(50);        

        $form = $this->creatFormBuilder()
            ->add('users',EntityType::class,[
                'class' => 'AppBundle:User','choices' => $users,// $users are added here
                'multiple' => true,'expanded' => true,])
            ->getForm();

         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid()) {
             $users = $form->getData();
             $this->deleteUsers($user);

             // Load the now first 50 users
             $users = $this->loadUsersFromDB(50); 

             // How to add these users to the form??
         }

         return $this->render('AppBundle::user_list.html.twig',['form' => $form->createView()]);
    }
}

删除提交的用户后如何在表单中设置正确的用户?


编辑

要回答评论中的问题,我提供一个小例子:

  • 假设表单显示了前5位用户。
  • 第一次调用该页面时,不提交表单。控制器将加载以下第一批用户,并使用它们创建表单并显示页面:
    • 鲍勃
    • 爱丽丝
    • 迈克
    • 约翰
    • 比尔
  • 用户在页面上选择 Bob Alice 进行删除,然后提交表单。
  • 再次调用控制器,并加载前5个用户的列表以创建表单。此列表与以前完全相同。
  • 但是,由于现在已提交表单,因此将处理数据,并删除Bob和Alice。
  • 现在我们无法返回创建的表单,因为它仍然包含Bob和Alice的条目...
  • 我们必须再次运行$users = $this->loadUsersFromDB(5)才能获得正确的结果,例如
    • 迈克
    • 约翰
    • 比尔
    • 吉姆
    • 汤姆

问题:

  • 在创建表单之前,必须先加载用户列表,因为我们需要此列表来创建表单。
  • 必须创建表格,以便我们处理
  • 处理表单确实会更改用户列表
  • 因此,在处理完表格后,我们需要再次加载用户列表。

因此::在处理表单之前加载用户列表是必要的工作,因为在处理表单时列表会发生变化。

如何避免这项工作?

当然,如示例中那样加载5个用户也没什么大不了,但是在实际代码中,这可能是数百个实体,并且两次运行相同的代码可能会对性能产生重大影响。

lanshushumanyouji 回答:创建并处理表单后,如何设置Symfony EntityType表单字段的选择?

我认为您在评论中没有理解我的建议,因此我将在这里更详细地解释/现在我也是从笔记本电脑上写:)/

class SomeController {
    public function deleteUsersAction(Request request) {
        $users = $this->loadUsersFromDB(50);        

        $form = $this->creatFormBuilder()
            ->add('users',EntityType::class,[
                'class' => 'AppBundle:User','choices' => $users,// $users are added here
                'multiple' => true,'expanded' => true,])
            ->getForm();

         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid()) {
             $users = $form->getData();
             $this->deleteUsers($user);

             // Load the now first 50 users
             $users = $this->loadUsersFromDB(50); <-- you don't need this

             // Q: How to add these users to the form??
             // A: When you return a redirect response to the same page /you can also pass any query parameters if you had any filters and etc../
             //    This will ensure that the page is loaded properly with the new list of the users and Bob and Alice will NOT be in the list
             //    If you don't do it with a redirect and the user submits the form the browser remembers the data and if the users hits the refresh button of the browser it will resubmit the form and with your approach it may delete next 2 users /Mike and John/.
             return $this->redirectToRoute('YOUR_ROUTE',$request->query->all());
         }

         return $this->render('AppBundle::user_list.html.twig',['form' => $form->createView()]);
    }
}```
本文链接:https://www.f2er.com/3101042.html

大家都在问