title: doctrine query builder join adding table twice
body: <p>UPDATED at bottom:</p>
  
  <p>I am trying to do what should be a simple join between two tables.  I have a Gig table and a Venue table for a simple band site that I am building using Symfony2 (2.2).  It's my first time with Symfony2 and doctrine so it is possible I am going completely in the wrong direction.  I have created and populated the tables with DataFixtures and have verified that the ID relationships are correct. The problem I am getting is that the resulting DQL query has the Gig table referenced twice in the FROM section and that is causing me to get back several instances of the same record instead of the x number of records I am expecting.  I don't know what I am doing wrong for that to be happening.  Also, there may be an easier way of doing this but I am exploring all of my options since I am teaching myself Symfony2 in the process of building the site.</p>
  
  <p>The Gig table contains a venue_id pointing to a Venue table that is defined in the Gig entity as a ManyToOne relationship (shown below).  Using a doctrine findAll everything seems to work fine with the Venue class in the Gig Entity being populated correctly.  I am trying to create a flat view of a few of the most recent Gigs to be displayed on the front page so I figured I would try to use a Join and include only the fields I need.</p>
  
  <p>Here is the Repository Query:</p>
  
  <pre><code>public function getGigsWithLimit($maxGigs)
  {
      $qb = $this-&gt;createQueryBuilder('b')
          -&gt;select('
              g.gigDate,
              g.startTime,
              g.endTime,
              g.message1 as gig_message1,
              g.message2 as gig_message2,
              g.url,
              v.name,
              v.address1,
              v.address2,
              v.city,
              v.state,
              v.zip,
              v.phone,
              v.url as venue_url,
              v.message1 as venue_message1,
              v.message2 as venue_message2,
              v.message3 as venue_message3'
          )
          -&gt;from('WieldingBassBundle:Gig', 'g')
          -&gt;leftJoin('g.venue', 'v')
          -&gt;orderBy('g.gigDate', 'DESC')
          -&gt;setMaxResults($maxGigs);
  
      return $qb-&gt;getQuery()-&gt;getResult();
  
  }
  </code></pre>
  
  <p>Here is the DQL it creates:</p>
  
  <pre><code>SELECT 
    g0_.id AS id0, 
    g0_.gig_date AS gig_date1, 
    g0_.start_time AS start_time2, 
    g0_.end_time AS end_time3, 
    g0_.message1 AS message14, 
    g0_.message2 AS message25, 
    g0_.url AS url6, 
    v1_.name AS name7, 
    v1_.address1 AS address18, 
    v1_.address2 AS address29, 
    v1_.city AS city10, 
    v1_.state AS state11, 
    v1_.zip AS zip12, 
    v1_.phone AS phone13, 
    v1_.url AS url14, 
    v1_.message1 AS message115, 
    v1_.message2 AS message216, 
    v1_.message3 AS message317 
  FROM 
    Gig g2_, 
    Gig g0_ 
  LEFT JOIN 
    Venue v1_ ON g0_.venue_id = v1_.id 
  LIMIT 
    6
  </code></pre>
  
  <p>The <strong>Gig g2_</strong> is my problem.  If I delete it and execute the query everything is as expected. I don't know what is generating that.</p>
  
  <p>The first table Gigs Entity looks like this (I am leaving out the getters and setters):</p>
  
  <pre><code>/**
   * Gig
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Wielding\BassBundle\Entity\GigRepository")
   * @ORM\HasLifecycleCallbacks
   */
  class Gig
  {
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var \DateTime
   *
   * @ORM\Column(name="gig_date", type="date")
   */
  private $gigDate;
  
  /**
   * @var \DateTime
   *
   * @ORM\Column(name="start_time", type="datetime")
   */
  private $startTime;
  
  /**
   * @var \DateTime
   *
   * @ORM\Column(name="end_time", type="datetime")
   */
  private $endTime;
  
  /**
   * @var string
   *
   * @ORM\Column(name="message1", type="string", length=50, nullable=true)
   */
  private $message1;
  
  /**
   * @var string
   *
   * @ORM\Column(name="message2", type="string", length=50, nullable=true)
   */
  private $message2;
  
  /**
   * @var string
   *
   * @ORM\Column(name="url", type="string", length=128, nullable=true)
   */
  private $url;
  
  /**
   * @var integer
   *
   * @ORM\Column(name="venue_id", type="integer")
   */
  private $venueId;
  
  /**
   * @ORM\Column(type="datetime")
   */
  protected $created;
  
  /**
   * @ORM\Column(type="datetime")
   */
  protected $updated;
  
  
  /**
   * @ORM\ManyToOne(targetEntity="Venue", cascade="persist")
   * @ORM\JoinColumn(name="venue_id", referencedColumnName="id")
   */
  protected $venue;
  </code></pre>
  
  <p>The Venue table is simple and does not have any relationships defined so I will leave it out unless it is asked for.</p>
  
  <p>Any ideas?  Thanks for any help.</p>
  
  <p>Andrew</p>
  
  <p>I removed everything except what would recreate the problem and here is what I was left with:</p>
  
  <p>I simplified the repository method to:</p>
  
  <pre><code>public function getGigsWithLimit2($maxGigs)
  {
      $qb = $this-&gt;createQueryBuilder('a')
          -&gt;select('g.id')
          -&gt;from('WieldingBassBundle:Gig', 'g')
          -&gt;setMaxResults($maxGigs);
  
      return $qb-&gt;getQuery()-&gt;getResult();
  }
  </code></pre>
  
  <p>This now generates:</p>
  
  <pre><code>SELECT 
    g0_.id AS id0 
  FROM 
    Gig g1_, 
    Gig g0_ 
  LIMIT 
    6
  </code></pre>
  
  <p>There is that darn Gig g1_ problem again.  I got the "Explain Query" from the Symfony profiler and it shows:</p>
  
  <pre><code>id  select_type table   type    possible_keys   key key_len ref rows        Extra
  1   SIMPLE  g1_ index       IDX_ED7D664240A73EBA    4       9     Using index
  1   SIMPLE  g0_ index       IDX_ED7D664240A73EBA    4       9    Using index; Using join buffer
  </code></pre>
  
  <p>I don't pretend to know what that means but it shows both table entries with different information about how it was used.</p>