title: Symfony 2 - Add new article comment
body: <p>I'm trying to add comments to articles with Symfony 2 but I get a white page when executing the POST.</p>
  
  <p>Here is the controller :</p>
  
  <pre><code>public function articleAction($id)
  {
      $repository = $this-&gt;getDoctrine()
                          -&gt;getManager()
                          -&gt;getRepository('PortfolioPortfolioBundle:Article');
      $article = $repository-&gt;find($id);
      if($article === null)
      {
          throw $this-&gt;createNotFoundException('Article[id='.$id.'] inexistant.');
      }
  
      $comment = new Comment();
      $form = $this-&gt;createFormBuilder($comment)
          -&gt;add('autor',       'text')
          -&gt;add('content',     'textarea')
          -&gt;getForm();
  
      $request = $this-&gt;get('request');
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;bind($request);
          if ($form-&gt;isValid()) {
              $article-&gt;addComment($comment);
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($article);
              $em-&gt;persist($comment);
              $em-&gt;flush();
          }
      }
  
      return $this-&gt;render('PortfolioPortfolioBundle:Portfolio:article.html.twig', array(
      'article' =&gt; $article, "active" =&gt; "article", 'form' =&gt; $form-&gt;createView()
      ));
  }
  </code></pre>
  
  <p>Here is the article model :</p>
  
  <pre><code>public function addComment(\Portfolio\PortfolioBundle\Entity\Comment $comment)
  {
      $this-&gt;comments[] = $comment;
      $comments-&gt;setArticle($this);
      return $this;
  }
  
  public function removeComment(\Portfolio\PortfolioBundle\Entity\Comment $comment)
  {
      $this-&gt;comments-&gt;removeElement($comment);
  }
  
  public function getcomments()
  {
      return $this-&gt;comments;
  }
  </code></pre>
  
  <p>Here is the comment model :</p>
  
  <pre><code>&lt;?php
  
  namespace Portfolio\PortfolioBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Comment
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Portfolio\PortfolioBundle\Entity\CommentRepository")
   */
  class Comment
  {
  /**
  * @ORM\ManyToOne(targetEntity="Portfolio\PortfolioBundle\Entity\Article", inversedBy="comments")
  * @ORM\JoinColumn(nullable=false)
  */
  private $article;
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="autor", type="string", length=255)
   */
  private $autor;
  
  /**
   * @var \DateTime
   *
   * @ORM\Column(name="date", type="datetime")
   */
  private $date;
  
  /**
   * @var string
   *
   * @ORM\Column(name="content", type="text")
   */
  private $content;
  
  public function __construct()
  {
      $this-&gt;date = new \Datetime('now');
  }
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set autor
   *
   * @param string $autor
   * @return Comment
   */
  public function setAutor($autor)
  {
      $this-&gt;autor = $autor;
  
      return $this;
  }
  
  /**
   * Get autor
   *
   * @return string 
   */
  public function getAutor()
  {
      return $this-&gt;autor;
  }
  
  /**
   * Set date
   *
   * @param \DateTime $date
   * @return Comment
   */
  public function setDate($date)
  {
      $this-&gt;date = new \DateTime("now");
  
      return $this;
  }
  
  /**
   * Get date
   *
   * @return \DateTime 
   */
  public function getDate()
  {
      return $this-&gt;date;
  }
  
  /**
   * Set content
   *
   * @param string $content
   * @return Comment
   */
  public function setContent($content)
  {
      $this-&gt;content = $content;
  
      return $this;
  }
  
  /**
   * Get content
   *
   * @return string 
   */
  public function getContent()
  {
      return $this-&gt;content;
  }
  
  /**
   * Set article
   *
   * @param \Portfolio\PortfolioBundle\Entity\Article $article
   * @return Comment
   */
  public function setArticle(\Portfolio\PortfolioBundle\Entity\Article $article)
  {
      $this-&gt;article = $article;
  
      return $this;
  }
  
  /**
   * Get article
   *
   * @return \Portfolio\PortfolioBundle\Entity\Article 
   */
  public function getArticle()
  {
      return $this-&gt;article;
  }
  }
  </code></pre>
  
  <p>Here is the view :</p>
  
  <pre><code>&lt;form class="comment-text" method="post" {{ form_enctype(form) }}&gt;
                                      &lt;div class="controls-group col-md-10"&gt;
                                          &lt;div class="controls"&gt;
                                              &lt;!--&lt;textarea id="content" name="content" class="form-control" placeholder="Add a comment" rows="5"&gt;&lt;/textarea&gt;&lt;br/&gt;--&gt;
                                              {{ form_widget(form) }}
                                              &lt;button id="contact-submit" type="submit" class="btn btn-primary"&gt;Send&lt;/button&gt;                                             
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/form&gt;
  </code></pre>
  
  <p>Logs :</p>
  
  <pre><code>[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
  </code></pre>
  
  <p>[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". [] []</p>
  
  <pre><code>[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
  
  [2013-11-14 10:25:36] request.INFO: Matched route "Article" (parameters: "_controller": "Portfolio\PortfolioBundle\Controller\PortfolioController::articleAction", "id": "2", "_route": "Article") [] []
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] []
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger". [] [
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger". [] []
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". [] []
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". [] []
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". [] []
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". [] []
  
  [2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". [] []
  
  [2013-11-14 10:25:36] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.date AS date4, t0.autor AS autor5, t0.content AS content6 FROM Article t0 WHERE t0.id = ? ["2"] []
  </code></pre>
  
  <p>Apache error log:</p>
  
  <pre><code>[Thu Nov 14 11:47:59 2013] [error] [client ] PHP Fatal error:  Call to a member function setArticle() on a non-object in C:\\wamp\\www\\Symfony\\src\\Portfolio\\PortfolioBundle\\Entity\\Article.php on line 206, referer: /Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP Stack trace:, referer: /Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   1. {main}() C:\\wamp\\www\\Symfony\\web\\app_dev.php:0, referer: /Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   2. Symfony\\Component\\HttpKernel\\Kernel-&gt;handle() C:\\wamp\\www\\Symfony\\web\\app_dev.php:28, referer: Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   3. Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel-&gt;handle() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2249, referer: /Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   4. Symfony\\Component\\HttpKernel\\HttpKernel-&gt;handle() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2947, referer: /Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   5. Symfony\\Component\\HttpKernel\\HttpKernel-&gt;handleRaw() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2818, referer: Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   6. call_user_func_array() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2844, referer: Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   7. Portfolio\\PortfolioBundle\\Controller\\PortfolioController-&gt;articleAction() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2844, referer: Symfony/web/app_dev.php/article/2
  [Thu Nov 14 11:47:59 2013] [error] [client ] PHP   8. Portfolio\\PortfolioBundle\\Entity\\Article-&gt;addComment() C:\\wamp\\www\\Symfony\\src\\Portfolio\\PortfolioBundle\\Controller\\PortfolioController.php:173, referer: Symfony/web/app_dev.php/article/2
  </code></pre>
  
  <p>Do you see any mistake ?</p>
  
  <p>Thank you very much !</p>