在Symfony中按ID删除单个条令条目

在Symfony中按ID删除单个条令条目,symfony,doctrine,Symfony,Doctrine,我正在尝试建立一个博客,我有一个所有博客条目的列表以及该博客的所有用户的列表。博客管理员可以从这些列表中删除单个条目。 无论如何,我有一个问题,当选择一个要删除的条目时,总是列表中第一个被删除的条目,而不是我实际选择的条目 以下是我的行动: /** * @Route("/blog/delete/{id}", name="entrydelete", requirements={"id" = "\d+"}, defaults={"id" = 0}) * */ pu

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

以下是我的行动:

  /**
    * @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' => 'BlogBundle\Entity\Blog'
      ]);
    }
以及相应的细枝模板:

{% 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%}循环中时,可以渲染尽可能多的模态元素

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

在您的代码中还有其他可以修复和优化的东西,但是为了回答您的问题,这里有一个简单的修复

更改此行:

<a href= "#myModal" role="button" ...>Delete entry</a>
<div id="myModal" ...>
致:

这一行:

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

这样,您就可以为每个模态元素获得一个唯一的id,从而使页面正常工作,HTML也有效


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

如果实体管理器可以找到具有整数id的Blog对象,也可以找到具有Blog$id的Blog对象。point is$Blog应该是$id,因为在路由中定义了它:id

更改此部分:

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


谢谢迈克尔,这完美地解决了我的问题!我对任何可以添加的优化都持开放态度,所以如果您不太在意的话,请随时与我分享!很高兴它有帮助!一些优化可能与代码样式有关。我将尝试将其添加到我的答案中,并让您知道。@sonja我在此页面上重新格式化了您控制器的一些代码,并在页面底部添加了注释:
/**
* @Route("/blog/delete/{id}", name="entrydelete", requirements={"id" = "\d+"})
*
*/
public function deleteAction(Request $request, Blog $id)