title: Doctrine2 in ZF2 - DQL gives different result than findOneBy method
body: <p>I have this entity:</p>
  
  <pre><code>/**
   * User state.
   *
   * @ORM\Entity(repositoryClass="User\Repository\StateRepository")
   * @ORM\Table(name="user_state")
   */
  class State implements StateInterface
  {
      /** ONE-TO-ONE BIDIRECTIONAL, OWNING SIDE
       * @var User
       * @ORM\Id
       * @ORM\OneToOne(targetEntity="User\Entity\User", inversedBy="state")
       * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
       */
      protected $user;
  
      /** ONE-TO-ONE UNIDIRECTIONAL
       * @var \Application\Entity\State
       * @ORM\Id
       * @ORM\ManyToOne(targetEntity="Application\Entity\State", fetch="EAGER")
       * @ORM\JoinColumn(name="state_id", referencedColumnName="id")
       */
      protected $state;
  
      public function __construct(User $user, \Application\Entity\State $state)
      {
          $this-&gt;user = $user;
          $this-&gt;state = $state;
      }
  
      /********/
      /* USER */
      /********/
      /**
       * Get user
       *
       * @return User
       */
      public function getUser()
      {
          return $this-&gt;user;
      }
  
      /**
       * Set user
       *
       * @param User $user
       *
       * @return Email
       */
      public function setUser(User $user)
      {
          $this-&gt;user = $user;
  
          return $this;
      }
  
      /**********/
      /* STATUS */
      /**********/
      /**
       * Get state
       *
       * @return \Application\Entity\State
       */
      public function getState()
      {
          return $this-&gt;state;
      }
  
      /**
       * Set state
       *
       * @param \Application\Entity\State $state
       *
       * @return State
       */
      public function setState(\Application\Entity\State $state)
      {
          $this-&gt;state = $state;
  
          return $this;
      }
  
      /******/
      /* ID */
      /******/
      /**
       * Get id.
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;state-&gt;getId();
      }
  
      /**
       * Set id.
       *
       * @param int $id
       *
       * @return State
       */
      public function setId($id)
      {
          $this-&gt;state-&gt;setId((int) $id);
  
          return $this;
      }
  
      /********/
      /* NAME */
      /********/
      /**
       * Get name
       *
       * @return string
       */
      public function getName()
      {
          return $this-&gt;state-&gt;getName();
      }
  
      /**
       * Set name
       *
       * @param string $name
       *
       * @return State
       */
      public function setName($name)
      {
          $this-&gt;state-&gt;setName((string) $name);
  
          return $this;
      }
  }
  </code></pre>
  
  <p>And to fetch the entity I do this in my custom repository</p>
  
  <p><code>return self::findOneBy($params);</code></p>
  
  <p>Which results in this query: </p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT t0.user_id AS user_id1, t0.state_id AS state_id2, t3.id AS id4, t3.name AS name5 FROM user_state t0 LEFT JOIN state t3 ON t0.state_id = t3.id WHERE t0.user_id = ? LIMIT 1
  </code></pre>
  
  <p>Perfect! Exactly what I wanted and the related entity is eagerly fetched because of the <code>fetch="EAGER"</code> on the relationship.
  Now I want to achieve the same with DQL, so i do this:</p>
  
  <pre><code>$query = $this-&gt;_em-&gt;createQuery("SELECT us, s FROM 'User\Entity\State' us LEFT JOIN us.state s WHERE us.user = :user_id");
  $query-&gt;setParameters($params);
  $query-&gt;setFetchMode('User\Entity\State', 'state', ClassMetadata::FETCH_EAGER);
  return $query-&gt;getOneOrNullResult();
  </code></pre>
  
  <p>I get an error message with: <code>Call to a member function getId() on a non-object</code>
  Apparently the related entity is not properly loaded. When I check manually the <code>$state</code> turns out to be <code>NULL</code></p>
  
  <p>If I ommit the s in the query like this: </p>
  
  <pre><code>$query = $this-&gt;_em-&gt;createQuery("SELECT us FROM 'User\Entity\State' us LEFT JOIN us.state s WHERE us.user = :user_id");
  </code></pre>
  
  <p>Then it returns the result exactly how I want it, but it results in two queries (an additional query to get the related state entity), which obviously makes sense because the query is not regarded as a <code>fetch join</code> any longer, so it lazy loads the related entity.</p>
  
  <p>How can I get the same result (one query, one result) as I got with using the <code>findOneBy</code> method but than with using DQL?</p>