Join subquery with doctrine 2 DBAL
<p>I'm refactoring a Zend Framework <strong>2</strong> application to use doctrine 2.5 DBAL instead of Zend_DB (ZF1). I have the following Zend_Db query:</p>

<pre><code>$subSelect = $db-&gt;select()
    -&gt;from('user_survey_status_entries', array('userSurveyID', 'timestamp' =&gt; 'MIN(timestamp)'))
    -&gt;where('status = ?', UserSurveyStatus::ACCESSED)
    -&gt;group('userSurveyID');


$select = $db-&gt;select()
    // $selectColNames contains columns both from the main query and 
    // the subquery (e.g. firstAccess.timestamp AS dateFirstAccess).
    -&gt;from(array('us' =&gt; 'user_surveys'), $selectColNames)
    -&gt;joinLeft(array('firstAccess' =&gt; $subSelect), 'us.userSurveyID = firstAccess.userSurveyID', array())
    -&gt;where('us.surveyID = ?', $surveyID);
</code></pre>

<p>This results in the following MySQL query:</p>

<pre><code>SELECT `us`.`userSurveyID`, 
    // More columns from main query `us`
    `firstAccess`.`timestamp` AS `dateFirstAccess`
FROM `user_surveys` AS `us`
LEFT JOIN (
    SELECT `user_survey_status_entries`.`userSurveyID`, 
            MIN(timestamp) AS `timestamp` 
    FROM `user_survey_status_entries` 
    WHERE (status = 20) 
    GROUP BY `userSurveyID`
) AS `firstAccess` ON us.userSurveyID = firstAccess.userSurveyID 
WHERE (us.surveyID = '10')
</code></pre>

<p>I can't figure out how to join the subquery using the doctrine 2.5 query builder. In the main query, I need to select columns from the subquery.  </p>

<p>I have read <a href="https://groups.google.com/forum/#!topic/doctrine-user/0rNbXlD0E_8" rel="noreferrer">here</a> that doctrine does not support joining subqueries. If that's still true, can I write this query in another way using the SQL query builder of doctrine DBAL? Native SQL may not be a good solution for me, as this query will be dynamically extended later in the code.</p>