我在 SYmfony2 上做一个简单的博客引擎。
我有 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');
}
事实上,我只想获得与我的文章相关的评论,并且与标签相同:/
我不知道我做错了什么?一些帮助会很好:)
谢谢