2013-01-24 48 views
0

我在SYmfony2上做了一個簡單的Blog引擎。關係ManyToOne - 我的實體無法識別的字段

我有2個實體評論和文章誰是與ManyToOne評論方面有關。

// Comment.php 
/** 
* @ORM\ManyToOne(targetEntity="Am\BlogBundle\Entity\Article") 
* @ORM\JoinColumn(nullable=false) 
*/ 
private $article; 

當我嘗試刪除文章時,我想刪除與本文相關的所有評論。

//Am/BlogBundle/Controller/BlogController.php 

public function delArticleAction(Article $article) 
{ 

    $em = $this->getDoctrine() 
       ->getEntityManager(); 

    $list_tags = $em->getRepository('AmBlogBundle:Tag') 
         ->findAll(); 

    $list_comments = $em->getRepository('AmBlogBundle:Comment') 
         ->findBy(array('article_id'=>$article->getId()));     

    //In order to delete an article, we have to remove each object linked to an Article 
    foreach($list_tags as $value){ 
     $article->removeTags($value); 
    } 

    foreach($list_comments as $value){ 
     //We delete all the comments of an article 
     $em = $this->getDoctrine()->getManager(); 
     $em->remove($value); 
     $em->flush(); 
    } 

    // We remove the Article 
    $em = $this->getDoctrine()->getManager(); 
    $em->remove($article); 
    $em->flush(); 

    return $this->render('AmBlogBundle:Blog:delArticle.html.twig'); 
} 

其實,我想只得到評論綁在我的文章,去相同的具有標記:/

我不知道我究竟做錯了什麼?有些幫助將是不錯的:)

感謝

回答

0

您要添加onDelete =「CASCADE」在你的實體定義,這將被添加到數據庫中的外鍵,所以當你刪除文章,它將自動級聯刪除相關注釋。

+0

好了,但我也得到了鏈接到我的實體文章也將刪除綁在文章右邊的所有標籤實體標籤?沒關係我回答我自己= D謝謝 – hyptos

相關問題