Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count queries to database in Doctrine2

Tags:

doctrine-orm

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?

like image 698
SET Avatar asked Jan 23 '11 11:01

SET


2 Answers

$stack = new \Doctrine\DBAL\Logging\DebugStack();
$entityManager->getConfiguration()->setSQLLogger($stack);
// do stuff
var_dump($stack);
like image 110
beberlei Avatar answered Oct 21 '22 10:10

beberlei


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/

like image 37
malloc4k Avatar answered Oct 21 '22 09:10

malloc4k