2017-04-30 138 views
0

我想知道是否有人知道如何通过一种表格编辑编辑多条记录。此链接显示我正在寻找的确切内容:https://editor.datatables.net/examples/simple/multiRow.html。我已经创建了一个ajax调用,将我选择的记录的ID发送到我创建的editMultipleRecordsAction方法。从那里我迷路了。任何提示都将非常感谢(Symfony已经有这样的东西了吗?我应该为此重建一个新的FormType吗?我应该使用相同的FormType,但添加侦听器吗?我应该创建一个新的实体,其中我的id属性将是一个数组? ...)。顺便说一下,我只有一个实体。如何一次编辑多条记录

+0

忽略下面的相当愚蠢的答案,只是使用窗体集合:HTTP:// symfony.com/doc/current/form/form_collections.html – Cerad

+0

也许你可以在你的问题中添加更多的细节?那么你有一个实体导致多个记录?似乎有点奇怪。你的多行链接确保它看起来像你在一个表单上有多个相同类型的实体。 – Cerad

+0

@Mark:您可以在这里看到我是如何解决问题的:“Symfony 2.8:使用西班牙语创建和处理来自同一个实体的多个记录的表单”,链接:https://symfonyandme.com/2017/10/13/symfony-2-8-como-crear-y-procesar -un-formula-para-multiples-registros-de-una-misma-entidad/ –

回答

0

我认为最简单的方法来实现,这将是:

  • 遍历$request->request->get('data');
  • 从DB
  • 获取当前实体创建当前实体的新形式
  • 手动调用Form::submit()为每种形式
  • $form->isValid()验证(每次迭代)
  • $em->flush()循环后有理论计算设定的改变并保存到数据库

下面是一个未经测试的例子

public function editMultipleRecordsAction (Request $request) 
{ 
    // First we select what to do based on 'action' field (add/edit/delete) 
    switch ($request->request->get('action')) { 
     case 'edit': 

      // Fetch the payload 
      $data = $request->request->get('data'); 

      // Fetch all edited entities 
      $entities = $this->getDoctrine()->getRepository('AppBundle:Entity') 
       ->findBy(['id' => array_keys($data)]); 

      // Build a map of $id => $entity to use later 
      $map = []; 
      foreach ($entities as $entity) { 
       $map[$entity->getId()] = $entity; 
      } 

      if (array_keys($data) !== array_keys($map)) { 
       // Something went wrong, not all requested entities were fetched 
      } 

      // Loop over each edited entity 
      foreach ($data as $id => $form_data) { 
       // Create a form for the current entity (you can use createForm() as well, same thing) 
       $form = $this->createFormBuilder($map[$id]) 
        // Notice that the id field is not part of the form 
        ->add('name') 
        ->add('position') 
        // ->add(...) 
        ->getForm(); 

       // Manually submit the data for this form 
       $form->submit($form_data); 

       if (!$form->isValid()) { 
        // Validation error, do something... 
       } 
      } 

      // Now we have iterated over each entity and updated it using Symfony's Form component 
      // The only thing remaining is to tell Doctrine to calculate the change set and save it to the database 
      $this->getDoctrine()->getManager()->flush(); 

      // DataTables require that you return a JSON version of updated entities 
      // This will be different depending on what you use to serialize. 
      // The following example applies if you are using FOS Rest Bundle: 
      return $this->handleView($this->view(['data' => $entities])); 
     break; 

     case 'create': 
      // ... 
     break; 

     case 'remove': 
      // ... 
     break; 

     default: 
      throw new BadRequestHttpException(); 
    } 
} 
+0

你好:)但我在哪里“存储”我的多个id在表单中? – Marc

+0

@Marc表单中不需要ID作为字段。从你链接的文档中,我明白DataTables把行ID放在'$ request-> request-> get('data')'中作为**键**。您通过该ID获取实体并将实体传递给'createForm()' – Xymanek

+0

是的,但事情是我有多个ID(通过单个表单更新多个记录) – Marc