发布 Symfony 3.4“@ParamConverter 注释找不到对象”
Issue Symfony 3.4 “object not found by the @ParamConverter annotation”
当我尝试删除 table 的选定项目时,我在 Symfony 3.4 上遇到 "object not found by the @ParamConverter annotation" 问题。当我尝试使用 id ("findOneBy()")
获取 "spectacle" 时,我认为这是一个问题
这是我的代码 (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');
}
感谢您的回复!!
请尝试更改此设置:
<form method="delete"
对此:
<form method="post"
因为你得到的变量好像是 POST
动作而不是 DELETE
当我尝试删除 table 的选定项目时,我在 Symfony 3.4 上遇到 "object not found by the @ParamConverter annotation" 问题。当我尝试使用 id ("findOneBy()")
获取 "spectacle" 时,我认为这是一个问题这是我的代码 (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');
}
感谢您的回复!!
请尝试更改此设置:
<form method="delete"
对此:
<form method="post"
因为你得到的变量好像是 POST
动作而不是 DELETE