MacBook avec code sur un bureau

Symfony – Doctrine – How to see where an SQL query is executed

When using Doctrine, it is sometimes important to find where an SQL query is executed.

Just turn on profiling_collect_backtrace on the connection, in the development configuration (config/packages/dev/doctrine.yaml):

doctrine:
    dbal:
        connections:
            default:
                profiling_collect_backtrace: true

Keep it for development only: collecting the call stack on every query is expensive. It relies on profiling, which is on by default when %kernel.debug% is true. The full list of options is in the Doctrine configuration.

The stack trace then shows up in the profiler, Doctrine tab, when you expand a query.

Stack trace of a Doctrine SQL query in the Symfony profiler

See also