0

我正在使用 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;
4

2 回答 2

0

That seems to be a drop cascade.

Take a look at your entities. You probably have either a cascade={"remove"} at the relationship definition or a ON DELETE trigger in your database. This answer might help you too:

On delete cascade with doctrine2

于 2013-04-24T04:51:23.943 回答
0

正如Renan Ivo所说,您可能会遇到以下情况之一:

1)cascade={"remove"}在将删除类别记录的外部键(博客端)上也已定义2)在数据库级别
具有触发器。ON DELETE请也看看你的 SQL Schema

于 2013-04-24T07:03:13.180 回答