title: Symfony2 - Blog & Comment system - How to persist a Comment
body: <p>I am learning how to make a basic Blog in Symfony2 and have come to a standstill when it comes to adding the comment system.</p>
  
  <p>So far I can view the blog entries fine. Under a single blog post I am using Twig to render a form directly from a Controller:</p>
  
  <pre><code>{% render "PaulDemoBundle:Default:addComment" with { 'id': blog_entry.id } %}
  </code></pre>
  
  <p>As you can see I am populating the ID with the Id of the blog being viewed, in order to allow me to correctly attach the comment to the blog entry.</p>
  
  <p>Below is the Action in my Controller:</p>
  
  <pre><code>public function addCommentAction(Request $request, $id)
  {
      $add_comment = new Comment();
      $add_comment-&gt;setBlogId($id);
  
      $form = $this-&gt;createFormBuilder($add_comment)
              -&gt;add('blog_id', 'hidden')
              -&gt;add('author', 'text')
              -&gt;add('comment', 'textarea')
              -&gt;getForm();
  
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;bindRequest($request);
  
          if ($form-&gt;isValid()) {
              $comment = $form-&gt;getData();
              $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
              $em-&gt;persist($comment);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('_index'));
          }
      }
  
      return $this-&gt;render('PaulDemoBundle:Default:add_comment.html.twig', array('form' =&gt; $form-&gt;createView(), 'id' =&gt; $id));
  }
  </code></pre>
  
  <p>here you can see that the Controller can pickup the Request data and the blog_id ($id). The form is generated with a hidden field that holds the blog_id.</p>
  
  <p>Viewing the form HTML source, you get this:</p>
  
  <pre><code>&lt;form action="/Symfony/web/app_dev.php/add_comment/3" method="post" &gt;
  
  &lt;div id="form"&gt;
  &lt;input type="hidden" id="form__token" name="form[_token]" value="d4adb511709259d0df921a0a1b969cee0df83630" /&gt;
  &lt;input type="hidden" id="form_blog_id" name="form[blog_id]" value="3" /&gt;
  &lt;div&gt;&lt;label for="form_author" class=" required"&gt;Author&lt;/label&gt;&lt;input type="text" id="form_author" name="form[author]" required="required" value="" /&gt;&lt;/div&gt;
  &lt;div&gt;&lt;label for="form_comment" class=" required"&gt;Comment&lt;/label&gt;&lt;textarea id="form_comment" name="form[comment]" required="required"&gt;&lt;/textarea&gt;&lt;/div&gt;&lt;/div&gt;
  &lt;input type="submit" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Again, you can see that the blog_id has been passed through (3).</p>
  
  <p>Now, filling in the Author and Comment and hitting Submit, I get an error. It seems to think the blog_id is NULL. I have done a var_dump (and commented out the persist stuff) and the form request data shows that the blog_id is set to 3.</p>
  
  <p>I even typecast $id to (int) as the form data shows it as a string (this shouldnt be a problem as it should be made an INT when it is persisted to the DB.</p>
  
  <p>Can anyone see the problem?</p>
  
  <p>Basically I have a M:1 Comment:Blog relationship and I need to somehow tell the application what blog is being viewed and pass along the ID (which I am doing atm, but it's broken ofc!)</p>
  
  <p>edit: the var_dump:</p>
  
  <pre><code>object(Paul\DemoBundle\Entity\Comment)#67 (6) { 
  ["id":"Paul\DemoBundle\Entity\Comment":private]=&gt; NULL 
  ["blog_id":"Paul\DemoBundle\Entity\Comment":private]=&gt; string(1) "3"
  ["blog":"Paul\DemoBundle\Entity\Comment":private]=&gt; NULL 
  ["author":"Paul\DemoBundle\Entity\Comment":private]=&gt; string(8) "Mr Jones" 
  ["comment":"Paul\DemoBundle\Entity\Comment":private]=&gt; string(4) "test"
  ["created":"Paul\DemoBundle\Entity\Comment":private]=&gt; NULL } 
  </code></pre>