2017-08-07 65 views
0

我正在嘗試設置博客,並列出了所有博客條目以及該博客的所有用戶列表。博客的管理員可以從這些列表中刪除單個條目。 無論如何,我遇到的問題是,當選擇一個條目被刪除時,它總是我的列表中的第一個條目被刪除,而不是我實際選擇的條目。通過Symfony中的ID刪除單個主義條目

這裏是我的deleteAction:

/** 
    * @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不見了。

會很高興任何形式的幫助!

回答

0

實際上您的HTML代碼存在問題。由於模態渲染位於{% for %}循環內,因此您可以渲染儘可能多的模態。

除了它沒有真正優化渲染如此多的模態元素,它們都具有相同的myModal id。當您點擊「刪除條目」按鈕時,會顯示編號爲myModal的模式,但其中有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 %}循環中),以及更改URL以使用JavaScript刪除元素),但這不在此範圍之內信息。

+0

感謝邁克爾,完美地解決了我的問題!我願意爲可以添加的任何優化工作而開放,所以如果對你沒有太多的努力,請隨時與我分享。 – sonja

+0

很高興幫助!一些優化可能與代碼風格有關。我會盡力將其添加到我的答案中,並讓你知道。 –

+0

@sonja我在此頁面上重新格式化了一些控制器的代碼(在頁面底部的註釋):https://gist.github.com/michaelperrin/4771f2857fadfd43e2af6487efd986fa –

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你的實體,你是不是通過博客的對象,但編號

0

如果實體管理器可以找到具有整數ID的Blog對象,也可以使用Blog $id查找。點爲$博客應該是$ 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)