phpc.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
A server for PHP programmers & friends. Join us for discussions on the PHP programming language, frameworks, packages, tools, open source, tech, life, and more.

Administered by:

Server stats:

791
active users

I uncovered a need to perform a nested query recently, but didn't know how to do it using Doctrine DBAL. So, now that I've figured it out, I've blogged about it.

mwop.net/blog/2025-03-06-dbal-

mwop.net · SQL Nested Queries of Sub Queries with Doctrine DBAL
More from Matthew Weier O'Phinney

@mwop

I don't like the following sentence:

> When creating a SELECT using DBAL, you do something like this: $dbal->createQueryBuilder();

DBAL does not force you to use the queryBuilder.

You can just execute the SQL Query as is using fetchAllAssociative.
The queryBuilder is overused for non-dynamic queries in my opinion.

@bcremer The example in my blog post is partial, and doesn't tell the complete story - it's actually built dynamically based on a variety of parameters passed to the method, and some joins, columns, and group statements may be omitted or included as a result. I simplified it for the blog to zero in on the subquery.

Benjamin Cremer

@mwop Fair Enough. The QueryBuilder has it's use-cases. But in your blog post it reads like it's the only way to solve this.

@bcremer I've updated the post to clarify this.

To be honest, I often use heredoc SQL strings. Usually when I do that, however, I'll use straight PDO instead of introducing the DBAL; to me, the value of the DBAL is the query builder for dynamically generating queries.