我正在使用 Symfony2 处理这个界面,这里有博客和与之关联的类别,有时我们想删除不再有效的博客,所以我只想删除博客,我将此功能添加到控制器中,它确实删除了博客,但同时它删除了所有相关的类别:
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$blawg = $em->getRepository('BlogBundle:Blog')->find($id);
if (!$blog) {
throw $this->createNotFoundException('Unable to find Blog entity.');
}
$em->remove($blog);
$em->flush();
}
return $this->redirect($this->generateUrl('blogs_inactive'));
}
我该怎么做才能删除博客信息?在数据库中,我有以下表格(Synfony2 上的实体):博客、Feed、类别、博客文章。
博客实体:
/**
* @var Category[]
*
* @ORM\ManyToMany(targetEntity="Category", cascade={"all"})
* @ORM\JoinTable(name="blog_categories",
* joinColumns={@ORM\JoinColumn(name="blog_id", referencedColumnName="blog_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="category_id")}
* )
*/
private $categories;
/**
* @var Feed
* @ORM\OneToOne(targetEntity="Feed", mappedBy="blog", cascade={"all"})
*/
private $feed;
/**
* @var ArrayCollection $posts
* @ORM\OneToMany(targetEntity="BlogPost", mappedBy="blog")
*/
private $posts;
饲料实体:
/**
* @var Blawg
*
* @ORM\ManyToOne(targetEntity="Blog")
* @ORM\JoinColumn(name="blog_id", referencedColumnName="blog_id",
* onDelete="CASCADE")
*/
private $blawg;