Doctrine2 DBAL Exists query
<p>I would like to ask for your help with <code>Doctrine2 DBAL</code> query built with <code>QueryBuilder</code>. I'm used to ORM, but I think it's an overkill for such query which is being called in a listener.</p>

<p>I need a query with <code>SELECT EXISTS</code> and I don't know how I can construct it using DBAL <code>QueryBuilder</code>.</p>

<p>I have a subquery already created:</p>

<pre><code>$subQuery = $connection-&gt;createQueryBuilder();
$subQuery
    -&gt;select('o.id')
    -&gt;from('order', 'o')
    -&gt;leftJoin('o', 'payment', 'p')
    -&gt;where($subQuery-&gt;expr()-&gt;isNull('p.id'))
;
</code></pre>

<p>I basically want to check if there are any unpaid orders. I now have no idea how to build the <code>SELECT EXISTS</code> query? Can anyone point me in the right direction? I was thinking about something like this:</p>

<pre><code>$qb-&gt;select('EXISTS(?)')-&gt;setParameter($subQuery-&gt;getDQL())
</code></pre>

<p>Will that be the correct solution?</p>

<p>@EDIT</p>

<p>After a while of thinking I decided to use ORM instead. Unfortunately that did not work either, I'm getting an error:</p>

<p><code>line 0, col 7: Error: Expected known function, got 'EXISTS'</code></p>

<p>The DQL is:
<code>SELECT EXISTS(&lt;subquery here&gt;)</code></p>

<p>It is a bit weird considering that It has been build with QueryBuilder:</p>

<pre><code>/* @var $qb QueryBuilder */
$qb = $this-&gt;em-&gt;createQueryBuilder();
$qb
        -&gt;select($qb-&gt;expr()-&gt;exists($subQuery-&gt;getDQL()));
</code></pre>