title: Doctrine2: calling flush multiple times
body: <p>I have a question concerning <strong>Doctrine</strong> and entities on <strong>Symfony</strong> 2.3.</p>
  <p>According to the v2.3 <em>&quot;Documentation Book&quot;</em> chapter <em>&quot;Databases and Doctrine&quot;</em> &gt; <a href="https://symfony.com/doc/2.3/book/doctrine.html#saving-related-entities" rel="nofollow noreferrer">Saving Related Entities</a>, the example create simultaneously a new row in both <code>product</code> and <code>category</code> tables and associates <code> product.category_id</code>  value with the <code>id</code> of the new <strong>category</strong> item.</p>
  <p>The problem is that the controller action creates a new <strong>Product</strong> and a new <strong>Category</strong> anytime it is invoked!</p>
  <p>In order to just create a new <strong>product</strong> and associate its <code>category_id</code> with an existing <strong>category</strong> <code>id</code>, this is the <strong>routing.yml</strong> route:</p>
  <pre class="lang-yaml prettyprint-override"><code>acme_store_create_product_by_category:
  path:     /create/newproduct/{name}/{categoryId}
  defaults: { _controller: AcmeStoreBundle:Default:createProduct }
  </code></pre>
  <blockquote>
  <p>I made a test passing parameters via URL:<br>
  /web/store/create/newproduct/Kayak/12</p>
  </blockquote>
  <p>I did something like this which seems working fine:</p>
  <pre class="lang-php prettyprint-override"><code>public function createProductAction($name, $categoryId)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      if ( $em-&gt;getRepository(&quot;AcmeStoreBundle:Category&quot;)-&gt;findOneById($categoryId) ) {
  
          $product = new Product();
  
          $product-&gt;setName($name);
          $product-&gt;setPrice(220);
          $product-&gt;setDescription(&quot;This is just a test&quot;);
  
          $em-&gt;persist($product);
  
          $em-&gt;flush();
  
          $newproduct = $em-&gt;getRepository(&quot;AcmeStoreBundle:Product&quot;)-&gt;find($product-&gt;getId());
          /** Create new product and populate $newproduct with its data */
  
  
          $repository = $em-&gt;getRepository(&quot;AcmeStoreBundle:Category&quot;)-&gt;find($categoryId);
  
          $newproduct-&gt;setCategory($repository);
  
          $em-&gt;persist($newproduct);
  
          $em-&gt;flush();
          /** Update the id_category field of the new product with parameter $categoryId */
  
          //exit(\Doctrine\Common\Util\Debug::dump($product));
  
          return new Response('Create product ' . $name . ' with category id ' . $categoryId);
  
      } else {
  
          return new Response('It doesn\'t exists any category with id ' . $categoryId);
      }
  }
  </code></pre>
  <p>My doubt in this case is: Is it a good practice to invoke <code>flush()</code> method two times in the same <strong>action</strong> ? In this case I would like to create a new <strong>product</strong> selecting the related <strong>category</strong> from a &quot;list box&quot;.</p>
  <p>Thank you in advance!</p>