title: Doctrine Query Builder with joins resulting in: "Unknown column id in 'on clause'"
body: <h4>Summary</h4>
  <p>I'm trying to use Doctrine's Query Builder to make a SELECT that includes a number of joins.</p>
  <p>This works fine when all the joins are done through entity relationships, but when I introduce a join not through a relationship (<code>campaign_instance</code> below), it results in a bad MySQL query.</p>
  <p>The error is &quot;<code>Unknown column 'w3_.id' in 'on clause'</code>&quot;. <code>w3_</code> refers to a table that absolutely has an <code>id</code> column.</p>
  <h4>The code</h4>
  <p>This query builder:</p>
  <pre class="lang-php prettyprint-override"><code>$this-&gt;entity_manager-&gt;getRepository(CampaignStep::class)
      -&gt;createQueryBuilder('campgain_steps_available_for_completion')
      -&gt;select('step')
      -&gt;from(CampaignStep::class, 'step')
      -&gt;join('step.group', 'campaign_group')
      -&gt;join('campaign_group.campaign', 'campaign')
      -&gt;join(
          CampaignInstance::class,
          'campaign_instance',
          'WITH',
          'campaign_instance.campaign = campaign')
      -&gt;getQuery()
      -&gt;execute();
  </code></pre>
  <p>results in this MySQL query:</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT
    t0_.id AS id_0,
    t0_.slug AS slug_1,
    t0_.description AS description_2,
    t0_.active AS active_3,
    t0_.created_at AS created_at_4,
    t0_.step_completion_min AS step_completion_min_5,
    t0_.ordinal AS ordinal_6,
    t0_.dtype AS dtype_7,
    t0_.mobile_image_id AS mobile_image_id_8,
    t0_.web_image_id AS web_image_id_9,
    t0_.group_id AS group_id_10
  FROM
    taxonomy_topic t1_
    INNER JOIN campaign_instance w2_ ON (w2_.campaign_id = w3_.id),
    taxonomy_topic t0_
    INNER JOIN campaign_group w4_ ON t0_.group_id = w4_.id
    INNER JOIN campaign w3_ ON w4_.campaign_id = w3_.id
    AND w3_.dtype IN ('campaign')
  WHERE
    (
      t1_.dtype IN ('campaignstep')
      AND t0_.dtype IN ('campaignstep')
    )
  </code></pre>
  <p>...Which results in this error:</p>
  <pre><code>Unknown column 'w3_.id' in 'on clause'
  </code></pre>
  <h4>Entity summary</h4>
  <p>Here are the entity relationships:</p>
  <p><code>Campaign</code> <code>&lt;-1-M-&gt;</code> <code>CampaignGroup</code> <code>&lt;-1-M-&gt;</code> <code>CampaignStep</code></p>
  <p><code>Campaign</code> <code>&lt;-1-M-</code> <code>CampaignInstance</code></p>
  <p>I'm selecting <code>CampaignStep</code>s, joining them to <code>CampaignGroup</code> then <code>Campaign</code> through their entity relationships. That much works.</p>
  <p>Then I want to join from <code>Campaign</code> to <code>CampaignInstance</code>. Since that relationship is unidirectional (owning side <code>CampaignInstance</code>), I can't use a <code>Campaign</code> relationship to get there. Adding this:</p>
  <pre class="lang-php prettyprint-override"><code>    -&gt;join(
          CampaignInstance::class,
          'campaign_instance',
          'WITH',
          'campaign_instance.campaign = campaign')
  </code></pre>
  <p>to the Query Builder breaks the resulting query.</p>
  <p>How do I fix this?</p>
  <h4>Packages</h4>
  <p><code>composer show doctrine/*</code>:</p>
  <pre><code>doctrine/annotations                1.13.2 Docblock Annotations Parser
  doctrine/cache                      1.12.1 PHP Doctrine Cache library is a popular cache implementation that supports ...
  doctrine/collections                1.6.8  PHP Doctrine Collections library that adds additional functionality on top ...
  doctrine/common                     2.13.3 PHP Doctrine Common project is a library that provides additional functiona...
  doctrine/dbal                       2.13.7 Powerful PHP database abstraction layer (DBAL) with many features for datab...
  doctrine/deprecations               v0.5.3 A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging w...
  doctrine/doctrine-bundle            2.3.2  Symfony DoctrineBundle
  doctrine/doctrine-migrations-bundle 2.2.3  Symfony DoctrineMigrationsBundle
  doctrine/event-manager              1.1.1  The Doctrine Event Manager is a simple PHP event system that was built to b...
  doctrine/inflector                  1.4.4  PHP Doctrine Inflector is a small library that can perform string manipulat...
  doctrine/instantiator               1.4.0  A small, lightweight utility to instantiate objects in PHP without invoking...
  doctrine/lexer                      1.2.3  PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive D...
  doctrine/migrations                 2.3.5  PHP Doctrine Migrations project offer additional functionality on top of th...
  doctrine/orm                        2.7.5  Object-Relational-Mapper for PHP
  doctrine/persistence                1.3.8  The Doctrine Persistence project is a set of shared interfaces and function...
  doctrine/reflection                 1.2.2  The Doctrine Reflection project is a simple library used by the various Doc...
  doctrine/sql-formatter              1.1.2  a PHP SQL highlighting library
  </code></pre>
  <hr />
  <h3>What I've tried so far:</h3>
  <p>This seems to have something to do with the multiple FROM clauses.</p>
  <p>If I remove the first clause <code>taxonomy_topic t1_ INNER JOIN workspace_instance w2_</code> from the FROM (and its related WHERE clause), I get a valid query that gives me exactly what I'm looking for:</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT
    t0_.id AS id_0,
    t0_.slug AS slug_1,
    t0_.description AS description_2,
    t0_.active AS active_3,
    t0_.created_at AS created_at_4,
    t0_.step_completion_min AS step_completion_min_5,
    t0_.ordinal AS ordinal_6,
    t0_.dtype AS dtype_7,
    t0_.mobile_image_id AS mobile_image_id_8,
    t0_.web_image_id AS web_image_id_9,
    t0_.group_id AS group_id_10
  FROM
    taxonomy_topic t0_
    INNER JOIN workspace_group w4_ ON t0_.group_id = w4_.id
    INNER JOIN workspace w3_ ON w4_.campaign_id = w3_.id
    AND w3_.dtype IN ('campaign')
  WHERE
    (
      t0_.dtype IN ('campaignstep')
    )
  </code></pre>
  <p>However, I don't know how to alter the Doctrine Query Builder to result in that MySQL.</p>