发出Symfony 3.4“@ParamConverter注释未找到的对象”

时间:2018-03-23 15:15:12

标签: php forms symfony symfony-3.4

当我尝试删除表中的所选项时,我在Symfony 3.4上遇到“@ParamConverter注释未找到对象”的问题。当我尝试用id(“findOneBy()”)获得“奇观”时,我认为这是一个问题

这是我的代码(html.twig):

<form method="delete" action="{{ path('admin_spectacle_delete_selected') }}">
<button class="content-red btn btn-fabop" type="submit"><i class="fa fa-trash"></i> Tout supprimer</button>

<div class="table-responsive">
    <table id="myTable" class="table table-bordered table-hover table-striped">
        <thead>
            <tr>
                <th style="text-align:center;"><input type="checkbox" id="all"></th>
                <th>Nom</th>
                <th>Lieu</th>
                <th>Date spectacle</th>
                <th>Annee</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
        {% for spectacle in spectacles %}
            <tr>
                <td id="spectacle{{ spectacle.id }}"><input type="checkbox" name='multiSelected[]' value="{{ spectacle.id }}"></td>
                <td>{{ spectacle.nom }}</td>
                <td>{{ spectacle.lieu }}</td>
                <td>{{ spectacle.dateSpectacle }}</td>
                <td>{{ spectacle.annee }}</td>
                <td>
                    <a class="content-blue btn-fabop btn" href="{{ path('admin_spectacle_show', { 'id': spectacle.id }) }}"><i class="fa fa-search"></i> Détail</a>
                    <a class="content-purple btn-fabop btn" href="{{ path('admin_spectacle_edit', { 'id': spectacle.id }) }}"><i class="fa fa-pencil"></i> Edition</a>
                    <a class="content-red btn-fabop btn" href="{{ path('admin_spectacle_delete_confirmed', { 'id': spectacle.id }) }}"><i class="fa fa-trash"></i> Suppression</a>


                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>

和控制器:

  /**
* Confirmation delete
*
* @Route("/deleteSelected", name="admin_spectacle_delete_selected")
*/
public function deleteSelectedAction(Request $request)
{

    $items_selected_id = $request->get('multiSelected');

      $em = $this->getDoctrine()->getManager();
      $repository = $em->getRepository(Spectacle::class);
      foreach($items_selected_id as $item_id) {

        $spectacle = $repository->findOneById($item_id);

        if (!$spectacle) {
            throw $this->createNotFoundException(
                'No spectacle found for id '.$spectacle
            );
        }
        else{
            $em->remove($spectacle);
        }
      }
      $em->flush();  
      return $this->redirectToRoute('admin_spectacle_index');
}

感谢您的回复!!

1 个答案:

答案 0 :(得分:0)

问题来自那里:

<form method="delete" action="admin_spectacle_delete_selected">

你小时候打电话给.../admin_spectacle_delete_selected,我假设你必须有一条路线/spectacle/{id}

所以你要匹配另一条尝试获得景象的路线&#34;其ID为&#34; admin_spectacle_delete_selected&#34;

您的表单应如下所示:

<form method="delete" action="{{ path('admin_spectacle_delete_selected') }}">

哪个匹配你的deleteSelectedAction

此外,你的行动应该像这样

  $items_selected_id = $request->get('multiSelected');

  $em = $this->getDoctrine()->getManager();
  $repository = $em->getRepository(Spectacle::class)
  foreach($items_selected_id as $item_id) {

    $spectacle = $repository->findOneById($item_id);

    if (!$spectacle) {
        throw $this->createNotFoundException(
            'No spectacle found for id '.$spectacle
        );
    }
    else{
        $em->remove($spectacle);
    }
  }
  $em->flush();  
  return $this->redirectToRoute('admin_spectacle_index');

您正在重新声明您的EntityManager / Flushing每个循环对我来说没有多大意义:)。