How can i get count of queries to database in Doctrine2? I need this just for statistic and to find out more how doctrine work, how much queries generated in different situations. But anyway, how to do this?
$stack = new \Doctrine\DBAL\Logging\DebugStack();
$entityManager->getConfiguration()->setSQLLogger($stack);
// do stuff
var_dump($stack);
Just to add to accepted answer.
To do this from the context of Symfony 2.x controller:
$doctrine = $this->get('doctrine');
$doctrine = $this->getDoctrine();
$em = $doctrine->getConnection();
// $doctrine->getManager() did not work for me
// (resulted in $stack->queries being empty array)
$stack = new \Doctrine\DBAL\Logging\DebugStack();
$em->getConfiguration()->setSQLLogger($stack);
... // do some queries
var_dump($stack->queries);
Thanks to this post: http://vvv.tobiassjosten.net/symfony/logging-doctrine-queries-in-symfony2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With