Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:25:01+00:00 2026-05-28T06:25:01+00:00

A quick question about saving data in symfony 2. I have this method (just

  • 0

A quick question about saving data in symfony 2. I have this method (just for testing):

protected function createProduct()
{
    $product = new Product();
    $product->setName('My product');
    $product->setDescription('Lorem ipsum dolor sit amet');
    $product->setIsPublished(1);
    $product->setPosition(1);


    $em = $this->getDoctrine()->getEntityManager();
    $em->persist($product);
    $em->flush();
}

Then i have the action (just for testing also):

public function indexAction()
{
    $this->createCategory();
    ...
    render ...
}

My problem is that when i execut the index action, the product is save twice in my database. Does anyone had similar problems ? Any way to solve it ?

Update: – full controller test class:

namespace Test\CategoryBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Test\CategoryBundle\Entity\Category;
use Symfony\Component\HttpFoundation\Response;

class CategoryController extends Controller
{
    public function createAction()
    {
        $c = new Category();
        $c->setName('Category');
        $c->setDescription('Lorem ipsum dolor sit amet');
        $c->setIsPublished(1);
        $c->setPosition(1);


        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($c);
        $em->flush();

        return new Response('Created category id '.$c->getId());

    }

}

Routing in src/Test/CategoryBundle/Resources/config/routing.yml:

TestCategoryBundle_create:
    pattern:  /category/create
    defaults: { _controller: TestCategoryBundle:Category:create }

Routing in app/config/routing.yml:

TestCategoryBundle:
    resource: "@TestCategoryBundle/Resources/config/routing.yml"
    prefix:   /
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:25:01+00:00Added an answer on May 28, 2026 at 6:25 am

    I found the problem. I don’t know yet if it’s normal, but at least the data is not duplicated anymore.

    The problem was solved by adding redirection to the createAction method. If you are not using a redirect, the data is duplicated. It’s normal ??? Anyway, this is the solution that worked for me.

    public function createAction()
    {
        $c = new Category();
        $c->setName('My Category');
        $c->setDescription('Lorem ipsum dolor sit amet');
        $c->setIsPublished(1);
        $c->setPosition(1);
    
        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($c);
        $em->flush();
    
        return $this->redirect($this->generateUrl('your_routing_name_to_redirect'));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick question about a function like this: class Test { public: Test(vector<int>&
I just have a quick question about how to get the length of a
I just have a quick question about the conditional operator. Still a budding programmer
I just have a quick question about how to generate id's on-the-fly for HTML
Just a quick question about JQuery's Ajax function, more specifically, the url parameter. My
I just have a quick question about IE8 Standards mode vs Quirks mode. The
I have a quick question about jQuery selectors. Is doing this: $('.class_el_1, .class_el_2').hide(); The
i just have a quick question about concurrent programming in Java. for example, i
I have a quick question about how to serve data from a repository in
Just a quick question about my Domain Layer/ Domain Service... Should I allow this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.