0

I'm working on this interface using Symfony2, here I've blogs and categories associated to them, sometimes we want to remove Blogs that are not longer valid, so I want to remove just the blog, I added this function to the controller, it does removes the Blog, but at the same time it removes all of the associated categories:

        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'));
        }

What can I do to just remove the Blog information? Inside the DB I've the following tables (Entities on Synfony2): Blog, Feed, Categories, Blog Posts.

Blog Entity:

/**
 * @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;

Feed Entity:

/**
 * @var Blawg
 *
 * @ORM\ManyToOne(targetEntity="Blog")
 * @ORM\JoinColumn(name="blog_id", referencedColumnName="blog_id",
 * onDelete="CASCADE")
 */
private $blawg;
Luis Franco
  • 782
  • 8
  • 13
  • Please post either your blog.orm.yml/feed.orm.yml/categories.orm.yml if you have them or the entity codes if you are using annotations. – Lighthart Apr 24 '13 at 00:32

2 Answers2

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

Community
  • 1
  • 1
Renan Ivo
  • 1,368
  • 9
  • 17
0

As Renan Ivo says, you could have one of those situation:

1) cascade={"remove"} onto external key (blog side) that will drop category record also have defined
2) have an ON DELETE trigger at data base level. Please, take a look at your SQL Schema also

Community
  • 1
  • 1
DonCallisto
  • 29,419
  • 9
  • 72
  • 100