Debug Doctrine SQL query with parameters

Sometimes you need to profile your queries with EXPLAIN to see what makes it slow. To do this you need a real SQL query, not DQL. Of course you can take it from Symfony Debug page, but it won't contain binded data, only "?" marks and the parameters that Doctrine binds. It is quite boring to insert them manually, you loose your time on this, can make a mistake etc.
I wrote a simple logger, that can log query with inserted parameters. Installation:
composer require --dev cmyker/doctrine-sql-logger:dev-master
Usage:
$connection = $this->getEntityManager()->getConnection(); 
$logger = new \Cmyker\DoctrineSqlLogger\Logger($connection);
$connection->getConfiguration()->setSQLLogger($logger);
//some query here
echo $logger->lastQuery; //or see the output

Comments

Popular posts from this blog

Memory efficient array permutation in PHP 5.5 using generators

How to dump http request headers with PHP under CGI/FastCGI SAPI

Zend Framework 2 AJAX: return JSON response from controller action. The proper way.