简体   繁体   中英

Issue Symfony 3.4 “object not found by the @ParamConverter annotation”

I have an issue with "object not found by the @ParamConverter annotation" on Symfony 3.4 when I try to delete selected items of table. I think it's an issue when I try to get the "spectacle" with the id ("findOneBy()")

This is my code (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>

and controller :

  /**
* 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');
}

Thank you for your response !!

The issue come from there:

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

You're litterally calling .../admin_spectacle_delete_selected , i assume you must have a route /spectacle/{id}

So you're matching another route trying to get a "spectacle" whose id is "admin_spectacle_delete_selected"

Your form should look like this:

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

Which would match your deleteSelectedAction

Also your Action should look like something like this

  $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');

You are redeclaring your EntityManager / Flushing each loop which doesn't make much sense to me :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM