简体   繁体   English

发行Symfony 3.4“ @ParamConverter批注未找到对象”

[英]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. 当我尝试删除表的选定项时,在Symfony 3.4上出现“ @ParamConverter注释找不到对象”的问题。 I think it's an issue when I try to get the "spectacle" with the id ("findOneBy()") 我认为当我尝试获取带有ID(“ findOneBy()”)的“眼镜”时,这是一个问题

This is my code (html.twig) : 这是我的代码(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} 您随便打电话给.../admin_spectacle_delete_selected ,我想您必须具有一条路线/spectacle/{id}

So you're matching another route trying to get a "spectacle" whose id is "admin_spectacle_delete_selected" 因此,您正在匹配另一条尝试获取ID为“ admin_spectacle_delete_selected”的“眼镜”的路线

Your form should look like this: 您的表单应如下所示:

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

Which would match your deleteSelectedAction 哪个与您的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 :). 您正在重新声明EntityManager /刷新每个循环,这对我来说意义不大:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony 4 - 仅 Profiler 中的 @ParamConverter 注释找不到对象 - Symfony 4 - Object not found by the @ParamConverter annotation in Profiler only Symfony 5 获取失败 object 未通过 @ParamConverter 注释找到 - Symfony 5 get fail object not found by the @ParamConverter annotation @paramconverter 注释 Symfony 找不到 App\\Entity\\Article 对象 - App\Entity\Article object not found by the @paramconverter annotation Symfony Symfony 4:创建添加实体表单时出错(@ParamConverter批注未找到对象) - Symfony 4 : Error creating an add entity form (object not found by the @ParamConverter annotation) 应用程序:使用 symfony 4 的 @ParamConverter 注释未找到发布 object - App:Post object not found by the @ParamConverter annotation using symfony 4 “@ParamConverter 注释未找到 App\Entity\User object” - "App\Entity\User object not found by the @ParamConverter annotation" @ParamConverter 注释找不到 App\Entity\PlaylistForCompany object - App\Entity\PlaylistForCompany object not found by the @ParamConverter annotation Symfony2 404错误:找不到对象(ParamConverter错误) - Symfony2 404 error: Object Not Found (ParamConverter error) Symfony 2或3 ParamConverter - Symfony 2 or 3 ParamConverter Symfony4。 ParamConverter注释与通过autowire注入服务冲突 - Symfony4. ParamConverter annotation conflicts injecting a service by autowire
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM