删除Symfony中按ID列出的单个教义条目



我正在尝试建立一个博客,我有一个所有博客条目的列表以及该博客的所有用户列表。博客管理员可以从这些列表中删除单个条目。 无论如何,我遇到的问题是,当选择要删除的一个条目时,它始终是列表中被删除的第一个条目,而不是我实际选择的条目。

这是我的删除操作:

/**
* @Route("/blog/delete/{id}", name="entrydelete", requirements={"id" = "d+"}, defaults={"id" = 0})
*
*/
public function deleteAction(Request $request, Blog $blog) {
$em = $this->getDoctrine()->getManager();
$entry = $em->getRepository('BlogBundle:Blog')->find($blog);
if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN') || $entry->getAuthor() == $this->getUser()->getUsername() ) {
$em->remove($entry);
$em->flush();
return $this->render('BlogBundle:blog:deletesubmit.html.twig');
}
else {
return $this->render('BlogBundle:blog:error.html.twig');
}
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'data_class' => 'BlogBundleEntityBlog'
]);
}

和根据树枝模板:

{% for blog in bloglist %}
<h4>{{ blog.title }}</h4>
<p><span class="fa fa-clock-o"></span> Posted on {{ blog.date|date('d.M Y H:i A') }} </p>
<p><span class="fa fa-user-circle"></span> Posted by {{ blog.author }} </p>
<p>{{ blog.text }}</p>
<button type="button" class="btn btn btn-info">
<a href="{{ path('entryedit', {'id':blog.id}) }}" style="color: #FEFEFE">Edit entry</a>
</button>
<button type="button" class="btn btn btn-warning">
<a href= "#myModal" role="button" data-toggle="modal" style="color: #FEFEFE">Delete entry</a>
</button>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title" id="myModalLabel">Are you sure?</h3>
</div>
<div class="modal-body">
<p>Do you really want to delete this profile?</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">
Go Back
</button>
<button type="button" class="btn btn btn-warning">
<a href="{{ path('entrydelete', {'id':blog.id}) }}" style="color: #FEFEFE">Yes</a>
</button>
</div>
</div>
</div>
</div>
<hr>
{% endfor %}

所以只是为了澄清这一点:如果我有这样的列表: 1. 条目 1 2. 条目 2 3. 条目 3 我想删除条目 3,选择那个并确认,条目 1 消失了。

会很高兴任何形式的帮助!

尝试更改它:

public function deleteAction(Request $request, Blog $blog) {
$em = $this->getDoctrine()->getManager();
$entry = $em->getRepository('BlogBundle:Blog')->find($blog);

对此:

public function deleteAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$entry = $em->getRepository('BlogBundle:Blog')->find($id);

因为您需要通过传递到 url 中的 id 来查找实体,所以传递的不是 Blog 对象,而是 id

您的 HTML 代码实际上存在问题。渲染的模态数量与{% for %}循环中的模态渲染量一样多。

除了渲染这么多模态元素并没有真正优化之外,它们都具有相同的myModalid。当您单击"删除条目"按钮时,将显示带有 idmyModal的模态,但其中有 3 个。您的浏览器显示第一个,这就是为什么它总是删除条目 1 的原因。

代码中还有其他可以修复和优化的内容,但要回答您的问题,这里有一个简单的修复。

更改此行:

<a href= "#myModal" role="button" ...>Delete entry</a>

自:

<a href="#deleteEntryModal{{ blog.id }}" role="button" ...>Delete entry</a>

还有这一行:

<div id="myModal" ...>

<div id="deleteEntryModal{{ blog.id }}" ...>

这样,您将获得每个模态元素的唯一ID,这使您的页面正常工作并且HTML也有效。

正如我之前所说,也可以进行一些优化(例如仅使用一个模态元素({% for %}循环之外(,并使用 JavaScript 更改 URL 以删除该元素。但这超出了此消息的范围。

如果实体管理器可以找到具有整数 id 的 Blog 对象,也可以找到具有Blog $id. 点$blog应该$id,因为在路由中您将其定义为:id

更改此部分:

/**
* @Route("/blog/delete/{id}", name="entrydelete", requirements={"id" = "d+"}, defaults={"id" = 0})
*
*/ 
public function deleteAction(Request $request, Blog $blog)

/**
* @Route("/blog/delete/{id}", name="entrydelete", requirements={"id" = "d+"})
*
*/
public function deleteAction(Request $request, Blog $id)

最新更新