title: How to get a collection of related entities by using Doctrine ResultSetMapping?
body: <p>I use Doctrine 2.3.4. and Symfony 2.3.0</p>
  
  <p>I have two entities: <code>Person</code> and <code>Application</code>. </p>
  
  <p>Application gets created when some Person applies for a job.</p>
  
  <p>Relation from <code>Person</code> to <code>Application</code> is <code>OneToMany</code>, bidirectional.</p>
  
  <p>Using the regular Doctrine documentation <a href="http://docs.doctrine-project.org/en/latest/reference/native-sql.html#the-nativequery-class" rel="noreferrer">here</a> I managed to get a correct result set only when working with a single entity. 
  However, when I add joined entity, I get a collection of root entities but joined to a wrong related entity.</p>
  
  <p>In other words, the problem is that I get a collection of Applications but all having the same Person.</p>
  
  <p>Native sql query, when executed directly returns a correct result.</p>
  
  <p>This is the code:</p>
  
  <pre><code>$sql = "SELECT a.id, a.job, p.first_name, p.last_name 
          FROM application a 
            INNER JOIN person p ON a.person_id = p.id";
  
      $rsm = new ResultSetMapping;
  
      $rsm-&gt;addEntityResult('\Company\Department\Domain\Model\Application', 'a');
      $rsm-&gt;addFieldResult('a','id','id');
      $rsm-&gt;addFieldResult('a','job','job');
  
      $rsm-&gt;addJoinedEntityResult('\Company\Department\Domain\Model\Person' , 'p', 'a', 'person');
  
      $rsm-&gt;addFieldResult('p','first_name','firstName');
      $rsm-&gt;addFieldResult('p','last_name','lastName');
  
      $query = $this-&gt;em-&gt;createNativeQuery($sql, $rsm);
  
      $result = $query-&gt;getResult();
  
      return $result;
  </code></pre>
  
  <p>Here are the Entity classes:</p>
  
  <pre><code>namespace Company\Department\Domain\Model;
  
  
  use Doctrine\ORM\Mapping as ORM;
  
  
  /**
  * Person
  *
  * @ORM\Entity
  * @ORM\Table(name="person")
  */
  
  class Person
  {
  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string First name
   *
   * @ORM\Column(name="first_name",type="string",length=255)
   */
  private $firstName;
  
  /**
   * @var string Last name
   *
   * @ORM\Column(name="last_name",type="string",length=255)
   */
  private $lastName;
  
  /**
  *
  * @var Applications[]
  * @ORM\OneToMany(targetEntity="Application", mappedBy="person")
  */
  private $applications;
  </code></pre>
  
  <p>Application class:</p>
  
  <pre><code>namespace Company\Department\Domain\Model;
  
  
  use Doctrine\ORM\Mapping as ORM;
  
  
  
  /**
  * Application (Person applied for a job)
  *
  * @ORM\Entity
  * @ORM\Table(name="application")
  */
  
  class Application
  {
  
  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
   private $id;
  
  /**
   * @var Person
   *
   * @ORM\ManyToOne(targetEntity="Person", inversedBy="applications")
   * @ORM\JoinColumn(name="person_id", referencedColumnName="id")
   */
  private $person;
  
  
  /**
   * @var string
   * @ORM\Column(name="job",type="string", length=100)
   */
  private $job;
  </code></pre>
  
  <p>I must be missing something here?</p>