title: Symfony doctrine repository (findOneBy) result is not object
body: <p>I try understand some weird behavior on the Doctrine side, for example:</p>
  
  <p>I have a simple form and send parameters in a request at the end create form.</p>
  
  <pre><code>/**
   * Displays a form to create a new Comment entity.
   *
   * @Route("/saveComment/", name="comment_new")
   * @Method("POST")
   * @Template()
   */
  public function newAction(Request $request)
  {
      $entity = new Comment();
      $form  = $this-&gt;createCreateForm($entity, $request);
  
      return array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      );
  }
  </code></pre>
  
  <p>Now I get my parameters and try to find the object in doctrine.</p>
  
  <pre><code>/**
   * Creates a form to create a Comment entity.
   *
   * @param Comment $entity The entity
   *
   * @return \Symfony\Component\Form\Form The form
   */
  private function createCreateForm(Comment $entity, Request $request)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $commentForUserRequest = $request-&gt;request-&gt;get('commentForUser');
      $commentForUser = $em-&gt;getRepository('ApplicationSonataUserBundle:User')-&gt;findOneBy(array('username' =&gt; $commentForUserRequest ));
  
      $auctionRequest = $request-&gt;request-&gt;getInt('auction');
  
      $auction = $em-&gt;getRepository('AppBundle:Auction')-&gt;find(1);  // **ALL FINE**
      $auction = $em-&gt;getRepository('AppBundle:Auction')-&gt;find($auctionRequest); //**PROBLEM**
  
      $auction-&gt;addComment($entity);
      $entity-&gt;setAuction($auction);
  
      $form = $this-&gt;createForm(new CommentType(), $entity, array(
                  'action' =&gt; $this-&gt;generateUrl('comment_create'),
                  'method' =&gt; 'POST',
      ));
  }
  </code></pre>
  
  <p>When I give one number instead of my request parameter, everything works fine.
  The following error message only occurs for the second case:</p>
  
  <blockquote>
    <p>Error: Call to a member function addComment() on a non-object</p>
  </blockquote>
  
  <p>I don't understand what I'm doing wrong.</p>
  
  <p>Edit:</p>
  
  <p><a href="https://i.stack.imgur.com/dQmSK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dQmSK.png" alt="Comment form"></a></p>
  
  <p><a href="https://i.stack.imgur.com/CUWgb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CUWgb.png" alt="When I try sumbint"></a></p>
  
  <p>And comment_create</p>
  
  <pre><code>/**
   * Creates a new Comment entity.
   *
   * @Route("/comment/", name="comment_create")
   * @Method("POST")
   * @Template("AppBundle:Comment:new.html.twig")
   */
  public function createAction(Request $request)
  {
  
      $entity = new Comment();
  
      $form = $this-&gt;createCreateForm($entity, $request);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('comment_show', array('id' =&gt; $entity-&gt;getId())));
      }
  
      return array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      );
  }
  </code></pre>