Adds a SQLCommenter comment to every statement Doctrine sends, naming the file, line and method in your code that issued it, and in a Symfony app the route and HTTP method of the request being handled. It is a DBAL driver middleware, so it covers the ORM, DQL, both query builders and raw DBAL calls alike.
A query issued at line 42 of /srv/app/src/Controller/OrderController.php, inside index,
while Symfony handles GET /customers/7/orders on the route /customers/{customer}/orders,
reaches the database as:
SELECT t0.id AS id_1 FROM orders t0 WHERE t0.customer_id = $1/*db_driver='doctrine',file='%2Fsrv%2Fapp%2Fsrc%2FController%2FOrderController.php%3A42',func_name='OrderController.index',method='GET',route='%2Fcustomers%2F%7Bcustomer%7D%2Forders'*/The values are encoded the way the other packages in this repository encode them, so a reader of one reads the other.
Register the middleware as an autowired service tagged doctrine.middleware:
# config/services.yaml
services:
QueryDoctor\SqlCommenter\Doctrine\Middleware:
autowire: true
tags: [doctrine.middleware]Autowiring passes the request stack and the router, which route and method are read from,
the cache.system pool and kernel.debug. Without it the middleware still tags file and
func_name. autowire: true is redundant when your _defaults already sets it, as Symfony's
default services.yaml does.
The tag applies it to every DBAL connection; { name: doctrine.middleware, connection: default }
limits it to one. With autoconfigure on, DoctrineBundle tags any service implementing
Doctrine\DBAL\Driver\Middleware by itself, and the explicit tag still registers it only once.
Pass the middleware in the DBAL configuration the connection is built with:
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use QueryDoctor\SqlCommenter\Doctrine\Middleware;
$config = new Configuration();
$config->setMiddlewares([new Middleware()]);
$connection = DriverManager::getConnection($params, $config);
$entityManager = new EntityManager($connection, $ormConfig);-
file: absolute path and line of the first stack frame that is your code. Skipped:- frames under
vendor/and from this package; - Doctrine's generated proxy classes (
__CG__*.php, in whatever proxy directory you set); - code Symfony compiles under
var/cache/: the container, lazy services and Twig templates. A query a template sets off is tagged with the controller line that rendered it; public/index.phpandbin/console: a query the framework issues itself, such as a session lookup, gets nofiletag.
PHP has no column.
- frames under
-
func_name:ShortClassName.method, or the function's name. A closure, such as the callback given towrapInTransaction(), is named after the method or function it is written in. Omitted at the top of a script and for a closure written there. Before PHP 8.4, a closure called after its function returned is not named either when that function is a plain function, or when the closure was bound to another class. -
route: in a Symfony app, the path pattern of the route that matched the request, asRoute::getPath()returns it:/users/{id}, never/users/42, with inline requirements and defaults such as{id<\d+>}left out. -
method: the request's HTTP method. -
db_driver: alwaysdoctrine.
route and method come from the main request on the request stack, so a sub-request (a
fragment, a forward) names the URL the client asked for. Symfony stores the matched route's name
on the request in _route; the middleware looks its pattern up in the router's route collection.
A statement sent when no request is being handled carries neither: a console command, a
Messenger worker, a kernel.terminate listener, or plain Doctrine without Symfony. Neither does
one sent during a request no route matched.
A lazy load is tagged with the line that touched the association, so an N+1 names the loop that causes it. With lazy ghosts (ORM 3, or ORM 2 with them enabled) and PHP 8.4's native lazy objects, a load starts inside the entity, where its getter first reads a property. That line is the same for every load of the entity, so the tag names the line that called the getter instead. Doctrine's own proxy classes in ORM 2 already start the load at that line.
A statement that already ends in a comment is sent unchanged, and a trailing ; stays last.
Each statement collects one backtrace without arguments. A native lazy load collects a second one with arguments, to tell which entity is loading.
In Symfony, the router builds its route collection by loading every routing file and controller
attribute, and it does so again for each new router, so once per request under PHP-FPM. For 500
attribute routes that took 4 to 9 ms per request (PHP 8.5 with OPcache, in Docker). The
middleware therefore keeps each route's name and pattern in cache.system, which Symfony
versions by container build, and loads the collection only when that entry is missing. In debug,
routes change without a container rebuild, so the cache is skipped and the collection is loaded
on the first statement of each request.
PHP 8.2 or later, DBAL 3.10 or 4. Tested against Postgres 17 with ORM 2.20 on DBAL 3, and ORM 3 on
DBAL 3 and 4, in each of the ORM's lazy-loading modes. route and method are tested in a
Symfony kernel with DoctrineBundle, on Symfony 6.4, 7.4 and 8.
composer install
vendor/bin/phpunitThe end-to-end tests need a Postgres with pg_stat_statements loaded, named in
SQLCOMMENTER_PG as user:password@host:port/db; without it they are skipped.
SQLCOMMENTER_LAZY_MODES (for example ghost,native) makes the run fail when the installed ORM
can't build one of the lazy-loading modes listed.
This package is developed in Query-Doctor/sqlcommenter, under php/sqlcommenter-doctrine. The standalone repository Packagist reads is a copy that a workflow updates on every merge, so send issues and pull requests to the monorepo. A release is a tag php/sqlcommenter-doctrine/vX.Y.Z on master, which the workflow turns into the tag vX.Y.Z here.